Will my LinqToSql execution be deffered if i filter with IEnumerable<T> instead of IQueryable<T>?
- by cottsak
I have been using these common EntityObjectFilters as a "pipes and filters" way to query from a collection a particular item with an ID:
public static class EntityObjectFilters
{
public static T WithID<T>(this IQueryable<T> qry,
int ID) where T : IEntityObject
{
return qry.SingleOrDefault<T>(item => item.ID == ID);
}
public static T WithID<T>(this IList<T> list,
int ID) where T : IEntityObject
{
return list.SingleOrDefault<T>(item => item.ID == ID);
}
}
..but i wondered to myself: "can i make this simpler by just creating an extension for all IEnumerable<T> types"?
So i came up with this:
public static class EntityObjectFilters
{
public static T WithID<T>(this IEnumerable<T> qry,
int ID) where T : IEntityObject
{
return qry.SingleOrDefault<T>(item => item.ID == ID);
}
}
Now while this appears to yield the same result, i want to know that when applied to IQueryable<T>s will the expression tree be passed to LinqToSql for evaluating as SQL code or will my qry be evaluated in it's entirety first, then iterated with Funcs?
I'm suspecting that (as per Richard's answer) the latter will be true which is obviously what i don't want. I want the same result, but the added benefit of the delayed SQL execution for IQueryable<T>s. Can someone confirm for me what will actually happen and provide simple explanation as to how it would work?