首页 > 解决方案 > 分组时,如何选择除 key 和 agregates 以外的列

问题描述

什么是 LINQ 等价物

SELECT primaryID, name, description, unit, SUM(price)
FROM Product
JOIN so that same Products are returned multiple times
GROUP BY primaryID

我知道可以使用 LINQ 进行 groupBy 查询,如下所示:

(from item in dbContext.products
 join some join statement that will make products return multiple times
 group item by item.primaryID into g
 select new {id = key, summedPrice = g.Sum(x => x.price)}
)

但是这样,我只能得到 ID 和聚合。我如何获得其他领域?

我想我可以让我的密钥成为一个包含我需要的所有列的 annon 类,但我注意到这是否会影响性能,如下所示:

group item by new { item.primaryID, item.name, item.description...} into g

标签: entity-frameworklinqentity-framework-core

解决方案


推荐阅读