Let's say I need an extension method which selects required properties from the collection. This can be entity or .net collection. So I have defined such extension method:
public IQueryable<TResult> SelectDynamic<TResult>(
this IQueryable<T> source,
This works fine for
IQueryables. But, I also need to call this function for
And in that case, I can call it with the help of
Both work fine. And I have such question, if both work fine, in which conditions I must create two different methods for the same purpose, which one works with
IEnumerable and the other works with
My method has to send query to the database in case of
For example, here is the source of
.Select extension method inside
I am repeating my main question again:
My method must send query to the database in case of
Queryable, but not when working with
IEnumerable. And for now, I am using
AsQueryable() for the enumerables. Because, I dont want to write same code for the
Enumerable. Can it have some side effects?
Best How To :
myEnumerable.AsQueryable() returns a custom object:
new EnumerableQuery<TElement>(myEnumerable); (source code)
EnumerableQuery class implements
When using the
EnumerableQuery result of
.AsQueryable() as an
IEnumerable, the implementation of the interface method
IEnumerable<T>.GetIterator() simply returns the original source iterator, so no change and minimal overhead.
When using the result of
.AsQueryable() as an
IQueriable, the implementation of the interface property
IQueriable.Expression simply returns
Expression.Constant(this), ready to be evaluated later as an IEnumerable when the whole expression tree is consumed.
(All the other methods and code paths of
EnumerableQuery are not really relevant, when the EnumerableQuery is constructed directly from an IEnumerable, as far as I can tell)
If I understand you correctly, you have implemented your method
selectDynamic<TResult>() in such a way that you construct an expression tree inside the method, that produces the desired result when compiled.
As far as I understand the source code, when you call e.g.
myEnumerable.AsEnumerable().selectDynamic().ToList(), the expression tree you constructed is compiled and executed on myEnumerable, and the total overhead should be fairly minimal, since all this work is only done once per query, not once per element.
So i think there is nothing wrong with implementing your IEnumerable Extension method like this:
public IEnumerable<TResult> SelectDynamic<TResult>(
this IEnumerable<T> source,...)
There is some slight overhead, since this compiles the query once each time this method is called, and I am not sure the JITer is smart enough to cache this compilation. But I think that will not be noticeable in most circumstances, unless you execute this query a thousand times per second.
There should be no other side efects, apart from slight performance issues, in implementing the IEnumerable extension method in this way.