简体   繁体   English

EFCore Linq 无法对包含聚合或子查询的表达式执行聚合 function

[英]EFCore Linq Cannot perform an aggregate function on an expression containing an aggregate or a subquery

I have a EFCore linq query that i try to get a sum of units, but i am getting an exception thrown.我有一个 EFCore linq 查询,我试图得到一个单位的总和,但我抛出了一个异常。

so here is the structure.所以这里是结构。

var query = _jobRepository.Table;

i am doing some filtering, nothing major, and then i have the includes.我正在做一些过滤,没什么大不了的,然后我有了包含。

query = query.Include(x => x.Plan)
    .ThenInclude(o => o.Listing)
    .ThenInclude(y => y.Product)
    .ThenInclude(a => a.AssembledProducts);

and at the end of the query, i want to get the sum units.在查询结束时,我想获得总和单位。

query.SumAsync(x => (x.Plan.Listing.Product.IsAssembled 
    ? x.Plan.Listing.Product.AssembledProducts.Sum(o => o.Qty) 
    : 1) * (x.Quantity * x.Plan.PackOf));

but i get an exception thrown Cannot perform an aggregate function on an expression containing an aggregate or a subquery.但我抛出异常无法对包含聚合或子查询的表达式执行聚合 function。

what would be the way to deal with this?处理这个问题的方法是什么?

Try to replace it with the following:尝试将其替换为以下内容:

 List<decimal> valuesList = await query.Select(x => (x.Plan.Listing.Product.IsAssembled 
    ? x.Plan.Listing.Product.AssembledProducts.Sum(o => o.Qty)  :1 )
     * (x.Quantity * x.Plan.PackOf)).ToListAsync();

decimal sumOfValues = valuesList.Sum();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM