If I have a query of the form
var query = from items in entityDb.TableName
join requestedBy in entityDb.People on items.RequestedById equals requestedBy.PersonId in requestedByOuter
from requestedBy in requestedByOuter.DefaultIfEmpty()
select items;
Also assume this is a big query, 10 more joins. Then I have a conditional filter
if (!string.IsNullOrEmpty(phaseFilter))
{
query = query.Where(item => item.Phase == phaseFilter);
}
At this point I believe I have lost the advantages of deferred execution. With 1000’s of results this can hurt performance. Considering my query is, in reality, 50 lines of code, I prefer not to go…
if(phaseFilter not null)
//50 lines....
else if(filter2 not null)
//50 lines...
else if (filter2 not null and phaseFilter not null)
//50 lines...
ugh. Can I somehow have my cake and eat it too?
Nope, you don’t lose it. The execution is still deferred, because
.Wherejust returns anIEnumerable<T>that filters theIEnumerable<T>passed to it, and it’s still deferred when you do that.