If I cast an IQueryable as an IEnumerable then call a Linq extension method, which implementation gets called?
Posted
by
James Morcom
on Stack Overflow
See other posts from Stack Overflow
or by James Morcom
Published on 2011-01-07T12:39:45Z
Indexed on
2011/01/07
12:54 UTC
Read the original article
Hit count: 180
Considering the following code:
IQueryable<T> queryable;
// something to instantiate queryable
var enumerable = (IEnumerable<T>) queryable;
var filtered = enumerable.Where(i => i > 3);
In the final line, which extension method gets called?
Is it IEnumerable<T>.Where(...)
? Or will IQueryable<T>.Where(...)
be called because the actual implementation is still obviously a queryable?
Presumably the ideal would be for the IQueryable version to be called, in the same way that normal polymorphism will always use the more specific override.
In Visual Studio though when I right-click on the Where method and "Go to Definition" I'm taken to the IEnumerable version, which kind of makes sense from a visual point-of-view.
My main concern is that if somewhere in my app I use Linq to NHibernate to get a Queryable, but I pass it around using an interface that uses the more general IEnumerable signature, I'll lose the wonders of deferred database execution!
© Stack Overflow or respective owner