Consider expression tree below,
var parameter = Expression.Parameter(typeof(Customer), "b");
var property = Expression.Property(parameter, "FirstName");
var abc = new string[] { "a", "b" };
MethodInfo contains = typeof(Enumerable)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Where(x => x.Name.Contains(nameof(Enumerable.Contains)))
.Single(x => x.GetParameters().Length == 2)
.MakeGenericMethod(property.Type);
var body = Expression.Call(contains, Expression.Constant(abc), property);
when we view this code in into debug we see the query generated by the body (variable) is:
.Call System.Linq.Enumerable.Contains(
.Constant<System.String[]>(System.String[]),
$b.FirstName)
and when we inspect the where in (var results = customers.Where(x => abc.Contains(x.FirstName)).ToList();
) we get this:
.Call System.Linq.Enumerable.Contains(
.Constant<Program+<>c__DisplayClass0_0>(Program+<>c__DisplayClass0_0).abc,
$x.FirstName)
How do you add the abc like in the where in the expression tree ?
Notes abc is: var abc = new string[] { "a", "b" }
(see code above)