LINQ Expression for Contains

后端 未结 1 1706
忘了有多久
忘了有多久 2021-01-12 14:20

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

相关标签:
1条回答
  • 2021-01-12 15:01

    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

    0 讨论(0)
提交回复
热议问题