views:

34

answers:

1

Is there a method call in Linq that will apply a expression tree directly on a List<V>? For instance, if I have a expression tree that is built on Order type, and i have a collection of List<Order> items on which i need to apply this expression.

I am looking something similar to:

class OrderListStore : IQueryable<V>,
    <other interfaces required to implement custom linq provider>
{
    List<Order> orders;

    public Expression Expression
    {
        get { return Expression.Constant(this); }
    }

    IEnumerator<V> IEnumerable<V>.GetEnumerator()
    {
        //Here I need to have a method that will take the existing expression
        //and directly apply on list
        something like this .. Expression.Translate(orders);
    }

}

Any help in this regard is highly appreciated.

+2  A: 

All of the interesting LINQ methods require a generic lambda expression - i.e. an Expression<SomeFuncType>, for example Expression<Func<T,bool>> for predicates (where), or Expression<Func<TSource,TValue>> for projections (select).

What you are doing sounds like Select, but you're going to have to give it a more specific lambda to be useful, and that doesn't work easily - you'd probably need a bit of MakeGenericMethod etc.

Re applying an Expression to LINQ-to-Objects, that is the easy bit. You can just use:

var projection = theList.AsQueryable().Select(lambda);

or

var filtered = theList.AsQueryable().Where(lambda);

(but note again that lambda there must be appropriately typed; Expression won't cut it).

(Alternatively, you can call .Compile() on the lambda and use regular LINQ-to-Objects)

Marc Gravell