public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
}
var persons = new List<Person>
{
new Person { Id = 1, FirstName = "Fox", LastName = "Mulder", Age = 40 },
new Person { Id = 2, FirstName = "Dana", LastName = "Scully", Age = 35 }
};
List<Person> Search(List<Person> persons, Person person)
{
//Here I'd like create a predicate here it's a AND between each.
//If !string.IsNullOrEmpty(person.FirstName) add to predicate, same for LastName
//If person.Age.HasValue() add to predicate
return persons.Where(myPredicateHere);
}
正如在Search
方法中所解释的,我想与AND合并,如果FirstName
不为null或空,它是 predicate 的一部分,对于LastName
也是如此,如果Age
有一个值,则将其添加到 predicate 中。
你知道吗?
谢谢你,
2条答案
按热度按时间9fkzdhlc1#
除了构建动态 predicate 之外,另一个选择是链接
Where
调用示例:https://dotnetfiddle.net/Xs4x8V
v6ylcynt2#
像这样的事?