我创建了Person实体
public class Person
{
public int Id {get;set;}
public string Fullname {get;set;}
public int Age {get;set;}
}
所以接下来我要过滤它。所以我也为Person做过readmodel。
public class PersonReadModel
{
public Expression<Func<int, bool>> Id {get;set;}
public Expression<Func<string, bool>> Fullname {get;set;}
public Expression<Func<int, bool>> Age {get;set;}
}
这里是实现
var filter = new PersonReadModel
{
Id = (id) => id > 100,
Age = (age) => age > 20
}
如您所见,我想过滤年龄超过20岁且ID大于100的人。
问题是如何将过滤器变量应用于where子句
_personRepository.GetAll().Where(? filter variable ?)
可以吗? 我希望我的解释是可以理解的,否则我很抱歉。
答案 0 :(得分:1)
根据您的情况,您可以像这样直接使用Where
var filter = _personRepository.GetAll().Where(x=>x.Id > 100 && x.Age>20);