How to specify dynamic field names in a Linq where clause?

前端 未结 3 1481
悲哀的现实
悲哀的现实 2020-12-01 10:09

If you create a Filter object that contains criteria for Linq that normally goes in a where clause like this:

 var myFilterObject = FilterFactory.GetBlank();         


        
3条回答
  •  被撕碎了的回忆
    2020-12-01 10:49

    You can build the Expression by hand, like so:

    var eParam = Expression.Parameter(typeof(Employee), "e");
    
    var comparison = Expression.Lambda(
        Expression.LessThan(
            Expression.Property(eParam, "Salary"),
            Expression.Constant(40000)),
        eParam);
    
    return from e in db.Employee.Where(comparison)
           select new EmployeeViewModel { Name = e.name, Salary = e.Salary };
    

提交回复
热议问题