I'm using DelegateDecompiler with Entity Framework to add computed properties. I'm also using PredicateBuilder to compose expressions for queries.
I'm having a problem getting the computed properties into SQL when using PredicateBuilder.
For Example:
Get Parents whose children's FullName starts with A, where FullName is a computed property
context.Parents.Where(p => p.Children.Any(c => c.Fullname.StartsWith("A"))).Decompile()
Works as desired.
I want to compose the Where Expression using A universal PredicateBuilder like so ...
var predicateParent = PredicateBuilder.True<ParentEntity>();
var predicateChild = PredicateBuilder.True<ChildEntity>();
predicateChild = predicateChild.And(c => c.FullName.StartsWith("A"));
predicateParent = predicateParent.And(x => x.Children.AsQueryable().Any(predicateChild));
var query = db.Parents.Where(predicateParent).Decompile();
List<ParentEntity> parents = query.ToList();
But it throws exception: The specified type member 'FullName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
How can the predicates be used with computed properties in children collection?
Here are the entity classes.
public class ParentEntity
{
public ParentEntity()
{
Children = new List<ChildEntity>();
}
public int Id { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public virtual List<ChildEntity> Children { get; set; }
[Computed]
[NotMapped]
public string FullName
{
get { return FirstName + " " + LastName; }
}
}
public class ChildEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
[Computed]
[NotMapped]
public string FullName
{
get { return FirstName + " " + LastName; }
}
public int ParentId { get; set; }
public virtual ParentEntity Parent { get; set; }
}
public class ParentChildModel: DbContext
{
public ParentChildModel()
: base("name=ParentChildModel")
{
}
public virtual DbSet<ParentEntity> Parents { get; set; }
public virtual DbSet<ChildEntity> Children { get; set; }
}