Print Page | Close Window

Filter on entities futher away on the business object path

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1536
Printed Date: 28-Apr-2025 at 12:15am


Topic: Filter on entities futher away on the business object path
Posted By: bigfish
Subject: Filter on entities futher away on the business object path
Date Posted: 29-Oct-2009 at 12:00am
I cannot define an entity query that filters entities that are futher away than "nextdoor" to the root entity.
 
Please consider this:
 
var query = manager.Orders
.Include("Customer")
.Include("OrderDetails")
.Include("OrderDetails.Product")
.Where(a => a.OrderDetails.Product.ProductName == "Duff Beer");
The properties of the Product entity are not exposed to filter on. ProductName is not exposed via intellisense.
 
Do you know of another method to achieve this instead ?
 
My real world need has yet another entity on the object graph to navigate to as well.  At this stage I am considering the creation of a sql view (though not preferred).
 
Thanks kindly



Replies:
Posted By: GregD
Date Posted: 30-Oct-2009 at 6:52am
Bigfish:

LINQ wants the subject in the Where clause to be a single property of the object currently being examined (an Order, in your query). It doesn't know how to resolve the expression "a.OrderDetails.Product", because that isn't a single property of Order.

You need to rewrite your query as follows:

var query = manager.OrderDetails
  .Where(od => od.Product.ProductName == "Duff Beer")
  .Select(od => od.Order)
    .Include("Customer")
    .Include("OrderDetails")
    .Include("OrderDetails.Product");

First you obtain the set of OrderDetails that meet your condition; then from that set, project the related Orders. Note that the Includes are specified in relation to the projected type (Order), not the type initially referenced in the query (OrderDetail).



Posted By: bigfish
Date Posted: 31-Oct-2009 at 1:03am
Thanks heaps Greg, much appreciated.  That helps a lot.


Posted By: GregD
Date Posted: 03-Nov-2009 at 11:28am
Quite welcome, bigfish.



Print Page | Close Window