按多列分组
我如何在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
使用匿名类型。
例如
group x by new { x.Column1, x.Column2 }
程序样本
.GroupBy(x => new { x.Column1, x.Column2 })
确定这是:
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();
链接地址: http://www.djcxy.com/p/27815.html