How do I build Expression Call for Any Method with generic parameter

◇◆丶佛笑我妖孽 提交于 2020-02-01 05:20:06

问题


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<...>>.



来源:https://stackoverflow.com/questions/34730432/how-do-i-build-expression-call-for-any-method-with-generic-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!