How do I build Expression Call for Any Method with

2019-04-12 11:56发布

问题:

I'm just trying make the same expression like below using Linq.Expression:

Expression<Func<Organization, bool>> expression = @org => 
    @org.OrganizationFields.Any(a =>
        a.CustomField.Name == field.Name &&
        values.Contains(a.Value));

In this example above I have an entity called Organization and it has a property called OrganizationsFields as IEnumerable and I want to find any occurrence that match with Any parameter expression.

I just using the code below to generate expression dynamically:

string[] values = filter.GetValuesOrDefault();

ParameterExpression parameter = Expression.Parameter(typeof(T), "org");
Expression organizationFields = Expression.Property(parameter, "OrganizationFields");

MethodInfo any = typeof(Enumerable)
   .GetMethods()
   .FirstOrDefault(a => a.Name == "Any" && a.GetParameters().Count() == 2)
   .MakeGenericMethod(typeof(OrganizationField));

Func<OrganizationField, bool> functionExpression = a =>
    a.CustomField.Name == filter.Name && values.Contains(a.Value);

Expression functionParam = Expression.Constant(
    functionExpression,
    typeof(Func<OrganizationField, bool>));

Expression call = Expression.Call(organizationFields, any, functionParam);

return Expression.Lambda<Func<T, bool>>(call, parameter);

The problems occur when I call the method Expression.Call it throw an ArgumentExeption

Can anyone help me?

Regards

回答1:

Here you go

var org = Expression.Parameter(typeof(Organization), "org");
Expression<Func<OrganizationField, bool>> predicate = 
    a => a.CustomField.Name == filter.Name && values.Contains(a.Value);
var body = Expression.Call(typeof(Enumerable), "Any", new[] { typeof(OrganizationField) },
    Expression.PropertyOrField(org, "OrganizationFields"), predicate);
var lambda = Expression.Lambda<Func<Organization, bool>>(body, org);

The essential part (covering your post title) is the following useful Expression.Call overload

public static MethodCallExpression Call(
    Type type,
    string methodName,
    Type[] typeArguments,
    params Expression[] arguments
)

Also note that the predicate argument of Any must be passed to Expression.Call as Expression<Func<...>>.