I have two entity types: Document
(has a Customer
) and Customer
(has a collection Documents
)
My query is to get documents for a customer based on either the customer's name or number.
The query looks like this:
public IQueryable<Document> GetCustomerDocuments(DateTime startDate, DateTime endDate, string filterText)
{
return this.ObjectContext.Customers
.Where(c => c.CustomerName.Contains(filterText) || c.CustomerNumber.Contains(filterText))
.SelectMany(c => c.Documents)
.Where(d => d.Date >= startDate && d.Date <= endDate);
}
When the query returns, I want it to include BOTH the Document
and Customer
entities....
I have tried everything I can think of including Include("Documents.Customer")
,Include("Customer")
,etc.
I definitely have the IncludeAttribute
set in the metadata.
Thoughts? Is this even possible?
Thanks!
.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:
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
Then your query becomes:
Instead of using projection and
SelectMany
, I wrote a LINQ query using a join:This solves the problem!