代码之家  ›  专栏  ›  技术社区  ›  M364M4N cro

分组时,如何选择键和aggregates以外的列

  •  0
  • M364M4N cro  · 技术社区  · 7 年前

    LINQ相当于什么

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

    (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
    
    0 回复  |  直到 7 年前