I have it:
public class Company { public int Id { get; set; } public string Name { get; set; } } public class City { public int Id { get; set; } public string Name { get; set; } public int ZipCode { get; set; } } public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int? Age { get; set; } public City City { get; set; } public Company Company { get; set; } }
I would like for some case to generate a predicate as follows:
var result = listPerson.Where(x => x.Age == 10).ToList<>();
Or that:
var result = listPerson.Where( x => x.Company.Name == 1234).ToList();
Or that:
var result = listPerson.Where( x => x.City.ZipCode == "MyZipCode").ToList();
Or that:
var result = listPerson.Where( x => x.Company.Name == "MyCompanyName").ToList();
Then I created a "PredicateBuilder" that works (I get the type if nullable or not, and I build a predicate) when I do this:
BuildPredicate<Person>("Age", 10); I get this : x => x.Age == 10
But I can not cope when there is a nested property like this:
BuildPredicate<Person>("City.ZipCode", "MyZipCode"); I'd like get this : x => x.City.ZipCode == "MyZipCode"
Or that:
BuildPredicate<Person>("City.Name", "MyName"); I'd like get this : x => x.City.Name == "MyName"
Or that:
BuildPredicate<Person>("Company.Name", "MyCompanyName"); I'd like get this : x => x.Company.Name == "MyCompanyName"
source share