我有这个 :
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; }
}
我想在某些情况下生成这样的谓词:
var result = listPerson.Where(x => x.Age == 10).ToList<>();
或这个 :
var result = listPerson.Where( x => x.Company.Name == 1234).ToList();
或这个 :
var result = listPerson.Where( x => x.City.ZipCode == "MyZipCode").ToList();
或这个 :
var result = listPerson.Where( x => x.Company.Name == "MyCompanyName").ToList();
然后我创建了一个“PredicateBuilder”,当我这样做时,这是工作(我得到类型,如果可以为空,我构建谓词):
BuildPredicate<Person>("Age", 10); I get this : x => x.Age == 10
但是当有这样的嵌套属性时,我不知道如何管理:
BuildPredicate<Person>("City.ZipCode", "MyZipCode");
I'd like get this : x => x.City.ZipCode == "MyZipCode"
或这个 :
BuildPredicate<Person>("City.Name", "MyName");
I'd like get this : x => x.City.Name == "MyName"
或这个 :
BuildPredicate<Person>("Company.Name", "MyCompanyName");
I'd like get this : x => x.Company.Name == "MyCompanyName"