Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can I do GroupBy multiple columns in LINQ

Something similar to this in SQL:

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


How can I turn this to 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
by

2 Answers

akshay1995
Use an anonymous type.

Eg

group x by new { x.Column1, x.Column2 }
kshitijrana14
Procedural sample:
.GroupBy(x => new { x.Column1, x.Column2 })

Login / Signup to Answer the Question.