访问表达式树中与子级(列表)相关的属性 [英] Access children(list) related property in Expression tree

查看:71
本文介绍了访问表达式树中与子级(列表)相关的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的实体Master创建了一个存储库.在存储库中,我有一个Get方法来使用实体核心通过ID获取我的实体.

I created a repository for my entity Master. In the repository, I have a Get method to get my entity by Id using Entity Core.

该方法收到:

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

然后,当我在代码中使用它时,我只是将要查找的实体的ID以及需要包含在查询中的相关实体的表达式树(Expression<Func<TEntity, object>>)传递给该方法

Then, when I use it in my code, I just pass to the method the id of the entity I´m looking for and and expression tree of the related entities that I need to include in the query (Expression<Func<TEntity, object>>)

使用示例如下:

var master = MasterRepository.Get(1, x => x.BranchOffice.Location);

在这种情况下,我正在寻找ID = 1的Master,并且希望它包含与BranchOffice相关的实体和与该BranchOffice相关的Location.

In that case I´m looking for the Master with Id = 1 and I want it to include the BranchOffice related entity and the Location related to that BranchOffice.

从一个到多个关系,它都能正常工作,但是对于相关列表,我不知道如何使用表达式来解决它.

From one to many relationships, it works fine, but for related lists, I dont know how to resolve it using an expression.

例如,如果我想包含与我的Master相关的名为DetailsDetail列表中的Product实体,我不知道如何在表达式树中表达它.

For example, if I want to include the Product entity of the list of Detail named Details related to my Master, I dont know how to express it in the expression tree.

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

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

Details is a list, so I cant access product as it is in the example above.

如何在Expression<Func<TEntity, object>>中表达出来?

How can I express that in a Expression<Func<TEntity, object>>?

我已经尝试过:

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

但是我遇到以下异常:

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

The property expression 'x => {from Detail y in [x].Details select [y].Product}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see go.microsoft.com/fwlink/?LinkID=746393.'

推荐答案

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

I don't know can you change or replace IncludeEntities implementations, so maybe answer would not be helpful for you. Well, x => x.Details.Product will looks like this DbContext.Set<SomeType>().Include(x => x.Details).ThenInclude(o => o.Product) in the EF.Core.

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

So if you want to include multiple levels I can suggest you to build a query at runtime that will contains Include and ThenInclude. So, this query will be built from input expression looks like this x => x.Details.Select(y => y.Product). It's method that build this query:

    /// <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;
    }

顺便说一句,方法采​​用一个表达式,但是可以对其进行少量修改,因此它将采用表达式数组,或者您可以从循环中直接为所有表达式调用该方法.

By the way, method takes a one expression, but it can be lightly modified, so it will takes array of expression or you can directly invoke the method from a loop for all of expressions.

下面的代码仅是用法.我写了一个树形的小类进行测试:

Code below is just usage. I wrote a tree small classes for testing:

    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();

我希望它将对您有帮助.

I hope it will helps you.

这篇关于访问表达式树中与子级(列表)相关的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆