How do I make this Query against EF Efficient?
Using EF4. Assume I have this:
IQueryable<ParentEntity> qry = myRepository.GetParentEntities();
Int32 n = 1;
What I want to do is this, but EF can't compare against null.
qry.Where( parent => parent.Children.Where( child => child.IntCol == n ) != null )
What works is this, but the SQL it produces (as you would imagine) is pretty inefficient:
qry.Where( parent => parent.Children.Where( child => chil开发者_运维知识库d.IntCol == n ).FirstOrDefault().IntCol == n )
How can I do something like the first comparison to null that won't be generating nested queries and so forth?
Try this:
qry.Where( parent => parent.Children.Any( child => child.IntCol == n ));
Any
in Linq translates to exists
in sql.
If I understood Your queries correctly, this is what You need.
精彩评论