Farhad Jabiyev Farhad Jabiyev - 1 year ago 115
C# Question

In which cases do I need to create two different extension methods for IEnumerable and IQueryable?

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
s. But, I have to call this function also 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 extension methods for the same purpose, whike one works with
and the other with

My method has to send query to the database in case of

For example, here is the source of
extension method inside

I am repeating my main question again:

My method must send query to the database in case of
, but not when working with
. And for now, I am using
for the enumerables. Because, I dont want to write same code for the
. Can it have some side effects?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download