SolarX
SolarX

Reputation: 1943

Create Expression from Func

I have a Func<TCollection, T> in my code. I use it to select certain properties.

In a call to another method I need Expression<Func<TCollection, T>> as a parameter.

Is there any way to convert (or create from) Func<TCollection, T> to Expression<Func<TCollection, T>> ?

Thanx

Upvotes: 65

Views: 70754

Answers (5)

F.&#199;etin
F.&#199;etin

Reputation: 11

Create a class like below;

 public class GenericFilter<T> where T : class, new()
    {
        public Expression<Func<T, bool>> Filter = item => true;

    }

Usage;

var filter=new GenericFilter<blabla>().Filter.And(x=>...);
filter=filter.And(x=>...);

Upvotes: 1

George Mavritsakis
George Mavritsakis

Reputation: 7083

You cannot create an expression from a delegate (from Func<TCollection, T> to Expression<Func<TCollection, T>>) but you can do the oposite.

That is, convert a Expression<Func<TCollection, T>> to a Func<TCollection, T> by compiling it (.compile).

So, if you need both of them you can use expressions at your functions and in case you need it, compile and execute them on a provided collection object.

We have to note of course that compiling an expression is slow.

Upvotes: 10

Simon
Simon

Reputation: 5493

You can do something like this:

Func<object, string> func = a => a.ToString();
Expression<Func<object, string>> expr = a => func(a);

But you will only get an expression containing your method call to the original Func. You won't be able to analyse the contains of the func itself.

Upvotes: 34

Jon Skeet
Jon Skeet

Reputation: 1500495

While you could just create an expression tree which calls your delegate, it's unlikely to be useful - because the delegate will basically be a black box as far as the code analyzing the expression tree is concerned. Assuming you're trying to use something like LINQ to SQL, the query analyzer will need to be able to peer into your logic to convert it to SQL - and it can't do that if it reaches a plain delegate.

You should probably change the code which comes up with the delegate in the first place, to create an expression tree instead.

Upvotes: 42

Polity
Polity

Reputation: 15130

You can not recreate an expression based on a method since an expression needs to know the original statements, not IL. You can however create an Expresson which makes a method call to your func like:

Func<int> func = () => 1;
Expression<Func<int>> expression = Expression.Lambda<Func<int>>(Expression.Call(func.Method));

Note however that systems like EF can't really work with this

Upvotes: 67

Related Questions