Access children(list) related property in Expressi

2019-07-14 19:34发布

问题:

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.

The method receives:

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

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

An example of use is the following one:

var master = MasterRepository.Get(1, x => x.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.

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.

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

EDIT:

I´ve already tried:

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

But I´m getting the following exception:

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.'

回答1:

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.

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.