gpt4 book ai didi

c# - 在表达式树中访问子(列表)相关属性

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:15 24 4
gpt4 key购买 nike

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

方法接收:

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

使用示例如下:

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

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

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

例如,如果我想包含 Product Detail列表实体名为 Details与我有关 Master , 不知道怎么用表达式树表达。

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

Details 是一个列表,所以我无法访问上面示例中的产品。

我如何在 Expression<Func<TEntity, object>> 中表达它? ?

编辑:

我已经试过了:

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

但我收到以下异常:

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实现,所以也许答案对你没有帮助。嗯,x => x.Details.Product看起来像这样DbContext.Set<SomeType>().Include(x => x.Details).ThenInclude(o => o.Product)EF.Core 中。

因此,如果您想包含多个级别,我建议您在运行时构建一个包含 Include 的查询和 ThenInclude .因此,此查询将从输入表达式构建,如下所示 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();

希望对您有所帮助。

关于c# - 在表达式树中访问子(列表)相关属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45887790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com