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

1

7

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

Spotless answered 11/1, 2016 at 20:25 Comment(2)
Can you provide details about the exception such as the message?Ligetti
Also, what is T in this case, can you show the method signature?Ligetti
O
13

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

Oulu answered 11/1, 2016 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.