How can I generate an Expression tree that queries an object with List<T> as a property?
- by David Robbins
Forgive my clumsy explanation, but I have a class that contains a List:
public class Document
{
public int OwnerId { get; set; }
public List<User> Users { get; set; }
public Document() { }
}
public class User
{
public string UserName { get; set; }
public string Department { get; set; }
}
Currently I use PredicateBuilder to perform dynmica queries on my objects. How can I turn the following LINQ statement into an Expression Tree:
var predicate= PredicateBuilder.True<User>();
predicate= predicate.And<User>(user => user.Deparment == "HR");
var deptDocs = documents.AsQueryable()
.Where(doc => doc.Users
.AsQueryable().Count(predicate) > 0)
.ToList();
In other words var deptDocs = documents.HasUserAttributes("Department", "HR").ToList();