c# - Efficient way to select records with children? -
i have linq sql query following...
return parent in context.parents ( (parent.someproperty != null && parent.someproperty != "") || (context.childs.count(c => c.parentid == parent.id && c.type == "sometype") > 0) ) select parent
the idea want find parent records have either got value "someproperty" or have child records of type "sometype".
the problem query timing out. there quicker (but still easy read) way of doing same thing?
thanks reading.
use any()
instead of count()
:
return parent in context.parents ( (parent.someproperty != null && parent.someproperty != "") || context.childs.any(c => c.parentid == parent.id && c.type == "sometype") ) select parent;
in linq sql count(<some condition>)
translated into:
select count(*) <some condition>
query requires iterating on rows in database find count.
in linq sql any(<some condition>)
translated a
exists (.. <some condition>)
subquery allows short circuiting result once first match found.
the exact sql mapping can found in answer: query result should use count() or any()
Comments
Post a Comment