在Expression树中访问children(list)相关属性

时间:2017-08-25 18:48:48

标签: c# lambda expression-trees

我为我的实体Master创建了一个存储库。在存储库中,我有Get方法通过Id使用Entity Core来获取我的实体。

该方法收到:

public TEntity Get(object id, params Expression<Func<TEntity, object>>[] includedRelatedEntities)
    {
        return GetById(IncludeEntities(DBContext.Set<TEntity>().AsQueryable(), includedRelatedEntities), id);
    }

然后,当我在我的代码中使用它时,我只是将我正在寻找的实体的id传递给方法,并且我需要在查询中包含相关实体的表达式树{{{1} })

使用的一个例子如下:

Expression<Func<TEntity, object>>

在这种情况下,我正在寻找Id = 1的Master,我希望它包含var master = MasterRepository.Get(1, x => x.BranchOffice.Location); 相关实体和与BranchOffice相关的Location

从一对多关系,它工作正常,但对于相关列表,我不知道如何使用表达式解决它。

例如,如果我想要包含与我的BranchOffice相关的名为Product的{​​{1}}列表的Detail实体,我不知道如何表达它表达树。

Details

详细信息是一个列表,因此我无法访问上面示例中的产品。

如何在Master

中表达这一点

修改

我已经尝试过了:

var master = MasterRepository.Get(1, x => x.Details.Product);

但我得到以下例外:

  

属性表达式'x =&gt; {来自[x]中的详细y。详细选择   [y] .Product}'无效。表达式应代表属性   访问:'t =&gt; t.MyProperty”。有关包括相关的更多信息   数据,请参阅go.microsoft.com/fwlink/?LinkID=746393。'

1 个答案:

答案 0 :(得分:1)

我不知道您是否可以更改或替换IncludeEntities实施,因此答案可能对您没有帮助。好吧,x => x.Details.Product EF.Core 中看起来像DbContext.Set<SomeType>().Include(x => x.Details).ThenInclude(o => o.Product)

因此,如果您想要包含多个级别,我建议您在运行时构建一个包含IncludeThenInclude的查询。因此,此查询将从输入表达式构建,如下所示x => x.Details.Select(y => y.Product)。它是构建此查询的方法:

    /// <summary>
    /// Takes include looks like 'x => x.Collections.Select(o => o.List.Select(p => p.Date))'
    /// </summary>
    public static IQueryable<T> GetQueryWithIncludes<T>(IQueryable<T> query, Expression<Func<T, object>> arg)
    {
        // Tiny optimization
        ParameterInfo[] parameters;
        var includeInfo = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(info => info.Name == "Include" &&
            (parameters = info.GetParameters()).Length == 2 &&
            typeof(Expression).IsAssignableFrom(parameters[1].ParameterType)).Single();

        // Retrieve then include that take first param as 'IIncludableQueryable<TEntity, ICollection<TPreviousProperty>>'
        var thenIncludeInfo = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(info => info.Name == "ThenInclude").ToList()[1];
        // Retrieve then include that take first param as 'IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>'
        var lastThenIncludeInfo = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(info => info.Name == "ThenInclude").ToList()[0];

        // Retrieve all selection from input expression
        var lambda = arg as LambdaExpression;
        var method = arg.Body as MethodCallExpression;
        var result = new List<Expression>();
        while (method != null)
        {
            result.Add(Expression.Lambda(method.Arguments[0], lambda.Parameters[0]));
            lambda = method.Arguments[1] as LambdaExpression;
            method = lambda.Body as MethodCallExpression;
        }
        result.Add(lambda);

        // Add Include and ThenInclude to IQueryable
        for (int i = 0; i < result.Count; ++i)
        {
            var lambdaExp = result[i] as LambdaExpression;
            query = i == 0
                ? includeInfo.MakeGenericMethod(lambdaExp.Parameters[0].Type, lambdaExp.ReturnType).Invoke(null, new object[] { query, lambdaExp }) as IQueryable<T>
                : i == result.Count - 1
                    ? lastThenIncludeInfo.MakeGenericMethod((result[0] as LambdaExpression).Parameters[0].Type, lambdaExp.Parameters[0].Type, lambdaExp.ReturnType).Invoke(null, new object[] { query, lambdaExp }) as IQueryable<T>
                    : thenIncludeInfo.MakeGenericMethod((result[0] as LambdaExpression).Parameters[0].Type, lambdaExp.Parameters[0].Type, lambdaExp.ReturnType).Invoke(null, new object[] { query, lambdaExp }) as IQueryable<T>;
        }
        return query;
    }

顺便说一句,方法接受一个表达式,但它可以轻微修改,因此它将采用表达式数组,或者您可以直接从循环中为所有表达式调用该方法。

下面的代码就是用法。我写了一个用于测试的树小类:

    public class Test
    {
        public int Id { get; set; }
        public DateTime TestDate { get; set; }
        public ICollection<Level> Levels { get; set; }
    }

    public class Level
    {
        public int Id { get; set; }
        public ICollection<LevelDetail> LevelDetails { get; set; }
    }

    public class LevelDetail
    {
        public int Id { get; set; }
        public DateTime LevelDate { get; set; }
    }

    ...
    // These results are the same and have the same expression trees
    var resultByInclude = context.Tests
        .Include(o => o.Levels)
        .ThenInclude(p => p.LevelDetails).ToList();

    var resultBySelect = GetQueryWithIncludes(context.Tests,
        o => o.Levels.Select(p => p.LevelDetails)).ToList();

我希望它会对你有所帮助。