Cannot convert lambda expression to input "System.Linq.Expressions.Expression" because it is not a delegate type

I am trying to create this lambda (x => x.MenuItemId)

but I am not sure how.

var item = Expression.Parameter(typeof(MenuItem), "x");
var prop = Expression.Property(item, "MenuItemId");
var lambda = Expression.Lambda<Func<MenuItem, object>>(x => x.MenuItemId);

      

+3


source to share


2 answers


You want to create a lambda with an element and recognize what you have already declared

var lambda = Expression.Lambda<Func<MenuItem, object>>(prop, item);

      

You can also hardcode the resulting lambda into the type x.MenuItemId

- if it is string

, it would be like this:

var lambda = Expression.Lambda<Func<MenuItem, string>>(prop, item);

      

You will call the resulting lambda with Compile()

and then use it where you would otherwise use the lambda.

For example, if you have a collection MenuItem

called items

and you would like everything MenuItemId

:



var compiled = lambda.Compiled(); 
var itemIds = items.Select(compiled); // roughly equivalent items.Select(x => x.MenuItemId);

      


I wrote a small utility class (ugh, I know) to wrap this functionality:

static class Gen<TModel, TProp> {
    public static Func<TModel, TProp> SelectorExpr(string propertyName) {
        var pExpr = Expression.Parameter(typeof (TModel));
        var mExpr = Expression.Property(pExpr, propertyName);
        var lExpr = Expression.Lambda<Func<TModel, TProp>>(mExpr, pExpr);
        return lExpr.Compile();
    }
}

      

Usage above:

var results = items.Select(Gen<MenuItem, object>.SelectorExpr("MenuItemId"));

      

+8


source


Just note, if you know what the lambda expression will look like at design time (rather than evaluate it based on variables at compile time), then the following code should make the expression tree you want:

Expression<Func<MenuItem, object>> lambda = x => x.MenuItemId;

      



You don't need to create expressions piece by piece if the pieces are always the same.;)

+4


source







All Articles