开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜