In EFCore 1.1 there's an .Include
var q = (from o in context.Company
where o.Id == CompanyId && !o.IsDelete
select o)
.Include(x => x.Personnel)
.ThenInclude(x => x.HomeAddress)
.First();
But the .Include
and .ThenInclude
only allows properties. I want to create an extension method modifies the original behavior to exclude IsDelete (Database is soft deleting)
public static IIncludableQueryable<TEntity, TProperty> AlongWith<TEntity, TProperty>(this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath) where TEntity : Domain.EntityBase
{
// original EFCore 1.1 .Include(..) behavior
var result = source.Include(navigationPropertyPath);
return result;
}
My goal is to create an Expression similar to navigationPropertyPath
, but include a check for IsDelete
// 1. Create the expression for IsDelete == false
var type = typeof(Domain.EntityBase);
ParameterExpression pe = Expression.Parameter(type, "personnel");
var left = Expression.Property(pe, type.GetProperty("IsDelete"));
var right = Expression.Constant(false);
var predicateBody = Expression.Equal(left, right);
// 2. Attempt to append the IsDelete check from step 1
var lambda = Expression.Lambda(navigationPropertyPath.Body, pe);
// error: System.InvalidOperationException
var navigationPropertyPathWithDeleteFalse = Expression.And(lambda, predicateBody);
Is it possible to append a && !IsDelete
when for the navigationPropertyPath?