问题
Let's say I need an extension method which selects only required properties from different sources. The source could be the database or in-memory collection. So I have defined such extension method:
public IQueryable<TResult> SelectDynamic<TResult>(
this IQueryable<T> source,
...)
This works fine for IQueryable
s. But, I have to call this function also for IEnumerable
s.
And in that case, I can call it with the help of .AsQueryable()
:
myEnumerable.AsQueryable()
.SelectDynamic(...)
.ToList();
Both work fine. And if both work fine, in which conditions I have to create two different extension methods for the same purpose, one for IEnumerable
and another one for IQueryable
?
My method has to send query to the database in case of Queryable
.
For example, here is the source of .Select
extension method inside System.Linq
namespace:
- .Select for IEnumerable
- .Select for IQueryable
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?
回答1:
myEnumerable.AsQueryable()
returns a custom object: new EnumerableQuery<TElement>(myEnumerable);
(source code)
This EnumerableQuery
class implements IEnumerable<T>
and IQueryable<T>
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,...)
return source.AsQueryable().SelectDynamic();
}
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.
回答2:
If your code only actually works when the objects its dealing with are loaded in memory, just supply the IEnumerable
variant and let your callers decide when they want to convert an IQueryable
into an in-memory IEnumerable
.
Generally, you won't implement new variations around IQueryable
unless you're writing a new database provider.
来源:https://stackoverflow.com/questions/30909228/in-which-cases-do-i-need-to-create-two-different-extension-methods-for-ienumerab