c# – 使用Expression创建带嵌套类的谓词

我有这个 :
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"

解决方法

(不打算复制Jon – OP联系我提供答案)

以下似乎工作正常:

static Expression<Func<T,bool>> BuildPredicate<T>(string member,object value) {
    var p = Expression.Parameter(typeof(T));
    Expression body = p;
    foreach (var subMember in member.Split('.')) {
        body = Expression.PropertyOrField(body,subMember);
    }
    return Expression.Lambda<Func<T,bool>>(Expression.Equal(
        body,Expression.Constant(value,body.Type)),p);
}

这和Jon的答案之间唯一的区别在于它通过告诉Expression.Constant预期的类型是什么来稍微处理null.作为使用示范:

static void Main() {
    var pred = BuildPredicate<Person>("City.Name","MyCity");

    var people = new[] {
        new Person { City = new City { Name = "Somewhere Else"} },new Person { City = new City { Name = "MyCity"} },};
    var person = people.AsQueryable().Single(pred);
}

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString(&quot;x2&quot;));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable&lt;Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include &quot;WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...