Creating a predicate with nested classes using an expression

I have it:

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 would like for some case to generate a predicate as follows:

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

Or that:

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

Or that:

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

Or that:

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

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

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

But I can not cope when there is a nested property like this:

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

Or that:

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

Or that:

 BuildPredicate<Person>("Company.Name", "MyCompanyName"); I'd like get this : x => x.Company.Name == "MyCompanyName" 
+6
source share
2 answers

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

Everything 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 this and John's answer is that he improves null bit by telling Expression.Constant what the expected type is. As a demonstration of use:

 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); } 
+9
source

You just need to divide the expression into dots and then iterate over it using Expression.Property several times. Something like that:

 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 answer. var rhs = Expression.Constant(targetValue, typeof(string)); var predicate = Expression.Equals(lhs, rhs); var lambda = Expression.Lambda<Func<T, bool>>(predicate, parameter); 
+5
source

All Articles