Create predicate with nested classes with Expression

匆匆过客 提交于 2019-12-03 00:37:50

(not intending to duplicate Jon - OP contacted me to provide an answer)

The following seems to work fine:

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);
}

The only functional difference between that and Jon's answer is that it handles null slightly better, by telling Expression.Constant what the expected type is. As a demonstration of usage:

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);
}

You just need to split your expression by dots, and then iterate over it, using Expression.Property multiple times. Something like this:

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!