简体   繁体   English

EF上父包含子的SQL查询

[英]SQL Query on Parent Include Child on EF

I am working on Silverlight with C# using RIA Services - MVVM pattern . 我正在使用RIA Services-MVVM模式在C#上使用Silverlight。 When I try to get records from Orders and OrdersDetails I get this error below: 当我尝试从OrdersOrdersDetails获取记录时,出现以下错误:

'Notes' is not a member of “注释”不是的成员
'Transient.collection[XXX.SilverLight.Web.Models.OrderDetails(Nullable=True,DefaultValue=)]'. 'Transient.collection [XXX.SilverLight.Web.Models.OrderDetails(Nullable = True,DefaultValue =)]'。
To extract a property of a collection element, use a subquery to iterate over the collection. 要提取集合元素的属性,请使用子查询来遍历集合。 Near simple identifier, line 6, column 58. 在简单标识符附近,第6行,第58列。

Here is my query: 这是我的查询:

public IQueryable<Order> AdvancedSearchOrder(string condition)
{
    ObjectQuery<Order> myQuery = new ObjectQuery<Order>("Orders", DbContext.ObjectContext()).Include("OrderDetails");
    if (condition != "")
    {
        myQuery = myQuery.Where(condition);
    }
    return myQuery;
}

In this case, I have 在这种情况下,我有

condition = "( (it.CustomerName like 'test')  )  and  ( (it.OrderDetails.Notes like 'testnote') )";

When I set it to 当我设置为

condition = "( (it.CustomerName like 'test')  )";

then, it works great. 然后,它很棒。

You are trying to use it.OrderDetails as a set, which it likely isn't given that it has a plural name. 您正在尝试使用它it.OrderDetails作为一个集合,可能没有给出它的复数名称。 I'm going to infer that your data model is each Order has one or more OrderDetail records. 我将推断您的数据模型是每个Order都有一个或多个OrderDetail记录。 The list of OrderDetail objects doesn't have a Notes property. OrderDetail对象的列表没有Notes属性。 An individual OrderDetail does. 一个单独的OrderDetail可以。 I'm not sure of the RIA syntax, but it should be something like this: 我不确定RIA语法,但是应该是这样的:

condition = "( (it.CustomerName like 'test')  )  and  ( 
      (it.OrderDetails.Notes.Any(n => n.Contains('testnote') 
)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM