3

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?

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
RJaxon
  • 41
  • 3
  • 1
    Possible duplicate of [EF: Include with where clause](https://stackoverflow.com/questions/16798796/ef-include-with-where-clause) – johnny 5 Mar 15 '18 at 19:40

0 Answers0