If I cast an IQueryable as an IEnumerable then call a Linq extension method, which implementation gets called?
- by James Morcom
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!