6

我有这个 :

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"
4

2 回答 2

9

(不打算复制 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);
}

这与乔恩的答案之间唯一的功能区别是它通过告诉预期类型是什么来处理null 得更好。Expression.Constant作为使用演示:

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);
}
于 2013-01-10T21:50:04.003 回答
5

你只需要用点分割你的表达式,然后迭代它,使用Expression.Property多次。像这样的东西:

string[] properties = path.Split('.');
var parameter = Expression.Parameter(typeof(T), "x");
var lhs = parameter;
foreach (var property in properties)
{
    lhs = Expression.Property(lhs, property);
}
// I've assumed that the target is a string, given the question. If that's
// not the case, look at Marc's answer.
var rhs  = Expression.Constant(targetValue, typeof(string));
var predicate = Expression.Equals(lhs, rhs);
var lambda = Expression.Lambda<Func<T, bool>>(predicate, parameter);
于 2013-01-10T13:23:27.943 回答