如何在LINQ中做GroupBy多列

SQL中类似的代码:

SELECT * FROM <TableName> GROUP BY <Column1>,<Column2>

如何将其转换为LINQ:

QuantityBreakdown
(
    MaterialID int,
    ProductID int,
    Quantity float
)

INSERT INTO @QuantityBreakdown (MaterialID, ProductID, Quantity)
SELECT MaterialID, ProductID, SUM(Quantity)
FROM @Transactions
GROUP BY MaterialID, ProductID

当前回答

程序样例:

.GroupBy(x => new { x.Column1, x.Column2 })

其他回答

var Results= query.GroupBy(f => new { /* add members here */  });

group x by new {x. col, x. col}

.GroupBy(x => (x.MaterialID, x. producd))

好的,这个是:

var query = (from t in Transactions
             group t by new {t.MaterialID, t.ProductID}
             into grp
                    select new
                    {
                        grp.Key.MaterialID,
                        grp.Key.ProductID,
                        Quantity = grp.Sum(t => t.Quantity)
                    }).ToList();

使用匿名类型。

Eg

group x by new { x.Column1, x.Column2 }