RIA Services : Include with Select(Many) not Working

懵懂的女人 提交于 2019-12-06 09:30:47

Instead of using projection and SelectMany, I wrote a LINQ query using a join:

        var v = from cust in (from c in this.ObjectContext.Customers
                where  (c.CustomerName.Contains(filterText) || c.CustomerNumber.Contains(filterText))  select c)
                join doc in this.ObjectContext.Documents on cust.CustomerNumber equals doc.CustomerNumber
                where doc.Date >= startDate && doc.Date <= endDate
                select doc;
        return ((ObjectQuery<Document>)v).Include("Customer").AsQueryable<Document>();

This solves the problem!

.Include works off ObjectQuery, and it's effect will be undone when adding any custom projection. You can try the following options:

Rewrite the query in terms of Document:

return this.ObjectContext.Documents.Include("Customers")
          .Where(d => d.Customers.Any(c => 
                                      c.CustomerName.Contains(filterText) 
                                   || c.CustomerNumber.Contains(filterText))
          .Where(d => d.Date >= startDate && d.Date <= endDate);

This may or may not work, and may or may not generate decent sql; to be tested.

Another possibility is to define a DTO object

class CustomerDocument
{
   public Customer {get;set;}
   public Document {get;set;}
}

Then your query becomes:

return from c in this.ObjectContext.Customers
       from d in c.Documents
       where (c.CustomerName.Contains(filterText) 
          || c.CustomerNumber.Contains(filterText))
           && d.Date >= startDate && d.Date <= endDate
       select new CustomerDocument {Customer = c, Document = d};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!