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);
}
As explained in the Search
method I'd like combine with a AND, if FirstName
is not null or empty it's part of the predicate, same for LastName
and if the Age
has a value add it to the predicate.
Any idea ?
Thanks,