在EFCore 1.1中,有.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();
但.Include
和.ThenInclude
仅允许属性。我想创建一个扩展方法来修改原始行为以排除IsDelete(数据库是软删除)
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;
}
我的目标是创建一个类似于navigationPropertyPath
的表达式,但包括对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);
是否可以为navigationPropertyPath附加&& !IsDelete
?