What I have is the following method. I use anonymous projection to filter the includes EF does. I learned this method from this blogpost: http://thedatafarm.com/data-access/use-projections-and-a-repository-to-fake-a-filtered-eager-load/
public IEnumerable<Entities.Nutrient> FindAllForSpecificLanguage(bool overridePossibleLogicalDelete)
{
using (var context = CreateObjectContext())
{
context.ContextOptions.LazyLoadingEnabled = false;
Entities.Nutrient[] result;
var list = context.Nutrients
.Select(nut => new
{
Entity = nut,
Descriptions = nut.Descriptions.Where(desc => desc.LanguageCode.Equals(DataLanguageContext.Current.DataLanguageCode))
}).ToList(); //perform query
var resultList = list
.Select(entity => entity.Entity);
return resultList;
}
}
This method should be built into all services (the api supports about 30 languages, at the moment we have a lot of DB overhead...). I'm trying to build it in a generic way, but I'm horribly inexperienced in expression trees. I thought I completely recreated the function, but I'm missing something because it isn't working. This is what I have so far:
public virtual IEnumerable<TEntity> FindAllForSpecificLanguage(bool overridePossibleLogicalDelete, Expression<Func<TEntity, IEnumerable<object>>> selectEntityDescriptions)
{
using (var context = CreateObjectContext())
{
context.ContextOptions.LazyLoadingEnabled = false;
ObjectQuery<TEntity> queryObjectSet = GetObjectSet(context);
TEntity[] result;
Type anonType = new {Entity = default(TEntity), Descriptions = Enumerable.Empty<object>()}.GetType();
// (entityManagerBaseEntity) => new { Entity = entityManagerBaseEntity, Descriptions = selectEntityDescriptions(entityManagerBaseEntity) }
// 1) "(entityManagerBaseEntity) =>"
var pe = Expression.Parameter(typeof(TEntity), "entityManagerBaseEntity");
// 2) "selectEntityDescriptions(entityManagerBaseEntity)"
var exprFunc = Expression.Invoke(selectEntityDescriptions, pe);
// get constructor for anonymous type
var constructorInfo = anonType.GetConstructor(new[] { typeof(TEntity), typeof(IEnumerable<object>) });
// 3) "new AnonType(entityManagerBaseEntity, exprFunc(entityManagerBaseEntity))"
var constructAnonType = Expression.New(constructorInfo, pe, exprFunc);
// 4) combine all to a lambda
// {entity => new <>f__AnonymousType0`2(entity, Invoke(entity => entity.Descriptions.Where(desc => desc.LanguageCode.Equals(DataLanguageContext.Current.DataLanguageCode)), entity))}
var cooleExpression = Expression.Lambda<Func<TEntity, dynamic>>(constructAnonType, pe);
//var bla = cooleExpression.Compile();
//var list = queryObjectSet.AsQueryable().Provider.CreateQuery<dynamic>(cooleExpression).ToList();
var list = queryObjectSet.Select(cooleExpression).ToList(); //perform query
var resultList = list
.Select(entity => entity.Entity as TEntity);
return resultList;
}
}
(note: CreateObjectContext and GetObjectSet are perfectly working methods)
Which should be called this way:
_nutrientManager.FindAllForSpecificLanguage(true, (entity) => entity.Descriptions.Where(desc => desc.LanguageCode.Equals(DataLanguageContext.Current.DataLanguageCode)))
The expression that gets built is typed in the comments. It looks fine I guess, but the join is never performed. If I debug I get the following stacktrace:
System.NotSupportedException: Only parameterless constructors and initializers are supported in LINQ to Entities. at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NewTranslator.TypedTranslate(ExpressionConverter parent, NewExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
So .. a few things:
the whole solution can be found here: https://github.com/YoeriVD/entity-framework-filter-on-include/blob/master/ExpressionTrees/Program.cs
the important piece is:
private static void Main(string[] args)
{
FilterOnInclude<Car, Wheel>(car => car.Wheels, wheel => wheel.SizeInInches == 14)
.ForEach(car => Console.WriteLine($"car : {car.Name} wheels: {car.Wheels.Count}"));
}
private static IEnumerable<TEntity> FilterOnInclude<TEntity, TChildEntity>(
Expression<Func<TEntity, IEnumerable<TChildEntity>>> propertyExpression,
Expression<Func<TChildEntity, bool>> predicateExpression)
where TEntity : class
{
using (var context = new CarContext())
{
context.Configuration.LazyLoadingEnabled = false;
var selector = CreateSelector(propertyExpression, predicateExpression);
return
context.Set<TEntity>().Select(
selector
).ToList().Select(e => e.Entity).ToArray();
}
}
private static Expression<Func<TEntity, EntityWithFilteredChildren<TEntity, TChildEntity>>> CreateSelector
<TEntity, TChildEntity>(
Expression<Func<TEntity, IEnumerable<TChildEntity>>> propertyExpression,
Expression<Func<TChildEntity, bool>> predicateExpression)
{
var selectType = typeof (EntityWithFilteredChildren<TEntity, TChildEntity>);
//bind entity
var entityValueParam = Expression.Parameter(typeof (TEntity), "entityValue");
var entityProp = selectType.GetProperty("Entity");
var entityValueAssignment = Expression.Bind(
entityProp, entityValueParam);
//bind collection
var childrenProp = selectType.GetProperty("Children");
var descriptionsMemberExpression = (propertyExpression.Body as MemberExpression);
var descriptionsPropertyInfo = (PropertyInfo) descriptionsMemberExpression.Member;
var descriptionsProperty = Expression.Property(entityValueParam, descriptionsPropertyInfo);
//perform where call
var whereCall = Expression.Call(typeof (Enumerable), "Where", new[] {typeof (TChildEntity)}, descriptionsProperty,
predicateExpression);
var descriptionValueAssignment = Expression.Bind(
childrenProp, whereCall);
var ctor = Expression.New(selectType);
var memberInit = Expression.MemberInit(ctor, entityValueAssignment, descriptionValueAssignment);
var selector = Expression.Lambda<Func<TEntity, EntityWithFilteredChildren<TEntity, TChildEntity>>>(memberInit,
entityValueParam);
return selector;
}
public class EntityWithFilteredChildren<T, TChild>
{
public T Entity { get; set; }
public IEnumerable<TChild> Children { get; set; }
}