Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create predicate with nested classes with Expression

I have this :

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'd like a some case generate the predicate like this :

var result = listPerson.Where(x => x.Age == 10).ToList<>();

Or this :

var result  = listPerson.Where( x => x.Company.Name == 1234).ToList();

Or this :

var result  = listPerson.Where( x => x.City.ZipCode == "MyZipCode").ToList();

Or this :

var result  = listPerson.Where( x => x.Company.Name == "MyCompanyName").ToList();

Then I created a "PredicateBuilder", that's work (I get the type, if nullable or not and I build the predicate) when I do this :

BuildPredicate<Person>("Age", 10); I get this : x => x.Age == 10

But I don't how manage when there is an nested property like this :

BuildPredicate<Person>("City.ZipCode", "MyZipCode"); 
I'd like get this : x => x.City.ZipCode == "MyZipCode"

Or this :

BuildPredicate<Person>("City.Name", "MyName"); 
I'd like get this : x => x.City.Name == "MyName"

Or this :

BuildPredicate<Person>("Company.Name", "MyCompanyName"); 
I'd like get this : x => x.Company.Name == "MyCompanyName"
like image 242
Kris-I Avatar asked Jan 10 '13 13:01

Kris-I


2 Answers

(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);
}
like image 79
Marc Gravell Avatar answered Nov 14 '22 23:11

Marc Gravell


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);
like image 35
Jon Skeet Avatar answered Nov 14 '22 22:11

Jon Skeet