How to filter Include in ObjectQuery<strong>Background:<br/></strong>ObjectQuery&lt;Customer&gt; myCollection = myEntityFrameworkContext.Customer.Include(&quot;Order.OrderDetail&quot;);<br/><br/>(Each Customer has a collection of Order objects and each Order has a collection of OrderDetail objects)<br/><br/><strong>Question</strong><br/>How can I filter the result or change the query in a way that only those resultsets are returned where OrderDetail with the productId = 2.<br/><br/>something like this: myEntityFrameworkContext.Customer.Include(&quot;Order.OrderDetails&quot;).Where(&quot;it.Order.OrderDetail.productId = 2&quot;)<br/><br/>Any help is much appreciated.<br/>© 2009 Microsoft Corporation. All rights reserved.Sat, 04 Jul 2009 20:31:22 Z3933061a-80a1-4c2a-9a7d-22aed7afc0e1http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#3933061a-80a1-4c2a-9a7d-22aed7afc0e1http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#3933061a-80a1-4c2a-9a7d-22aed7afc0e1Dynamichttp://social.msdn.microsoft.com/Profile/en-US/?user=DynamicHow to filter Include in ObjectQuery<strong>Background:<br/></strong>ObjectQuery&lt;Customer&gt; myCollection = myEntityFrameworkContext.Customer.Include(&quot;Order.OrderDetail&quot;);<br/><br/>(Each Customer has a collection of Order objects and each Order has a collection of OrderDetail objects)<br/><br/><strong>Question</strong><br/>How can I filter the result or change the query in a way that only those resultsets are returned where OrderDetail with the productId = 2.<br/><br/>something like this: myEntityFrameworkContext.Customer.Include(&quot;Order.OrderDetails&quot;).Where(&quot;it.Order.OrderDetail.productId = 2&quot;)<br/><br/>Any help is much appreciated.<br/>Thu, 07 May 2009 20:13:57 Z2009-05-07T20:13:57Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#5bf558cd-c8bf-441f-8d9c-5ef3f7ce8743http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#5bf558cd-c8bf-441f-8d9c-5ef3f7ce8743Dynamichttp://social.msdn.microsoft.com/Profile/en-US/?user=DynamicHow to filter Include in ObjectQueryI found the solution myself :)<br/><br/>myEntityFrameworkContext.Customer.Include(&quot;Order.OrderDetails&quot;).Where(customer =&gt; customer.Order.Any(order =&gt; order.OrderDetail.Any(orderDetail =&gt; orderDetail.ProductId ==2)));Fri, 08 May 2009 09:10:04 Z2009-05-08T09:10:04Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#14d83615-abdc-4690-b2eb-efa00c542498http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#14d83615-abdc-4690-b2eb-efa00c542498Dynamichttp://social.msdn.microsoft.com/Profile/en-US/?user=DynamicHow to filter Include in ObjectQuery<p>Well, I just noticed a problem with this.<br/><br/>Customer is filtered but other tables are returned entirely! (not filtered)<br/><br/>Any idea?</p>Fri, 03 Jul 2009 16:38:47 Z2009-07-03T16:38:47Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#eedb86d5-c93b-4028-a264-4cf1273950bdhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3933061a-80a1-4c2a-9a7d-22aed7afc0e1#eedb86d5-c93b-4028-a264-4cf1273950bdDynamichttp://social.msdn.microsoft.com/Profile/en-US/?user=DynamicHow to filter Include in ObjectQueryI think this is a bug with the Entity Framework or I don't know how to do it.<br/><br/>Anyway, the only way I found is to change the Object Query direction.<br/><br/><strong>The &quot;many&quot; side of the relationship must come first always:</strong><br/><br/>myEntityFrameworkContext.OrderDetail.Include(&quot;Order.Customer&quot;).Where(orderDetail.ProductId ==2);<br/><br/>Now only the Order and Customer are returned which are associated with the selected OrderDetail.<br/><br/>Now again I have to Convert the data to the format I need which means Customer.Order.OrderDetail not OrderDetail.Order.CustomerSat, 04 Jul 2009 20:31:01 Z2009-07-04T20:43:44Z