How do I make this Query against EF Efficient?

Posted by dudeNumber4 on Stack Overflow See other posts from Stack Overflow or by dudeNumber4
Published on 2010-04-22T23:22:26Z Indexed on 2010/04/22 23:23 UTC
Read the original article Hit count: 236

Filed under:

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 => child.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?

© Stack Overflow or respective owner

Related posts about entity-framework