I want to add dynamic expression in linq but facing issues on contains method it is working perfectly for Equal method
Problem is i\'m getting FilterField
This will work for you:
void Main()
{
var filterField = "Id";
List<int> Ids = new List<int>();
var eParam = Expression.Parameter(typeof(EmployeeDetail), "e");
var method = Ids.GetType().GetMethod("Contains");
var call = Expression.Call(Expression.Constant(Ids), method, Expression.Property(eParam, filterField));
var lambda = Expression.Lambda<Func<EmployeeDetail, bool>>(call, eParam);
}
public class EmployeeDetail
{
public int Id { get; set; }
}
First, you look for the Contains
method on the type of Ids
. Then we simply invoke it with Ids
as the instance, and the property as the argument