This post show you how to use linq group by multiple columns in c#.
var result =
from o in db
group o by (o.Column1, o.Column2) into g
select new (g.Key.Column1, g.Key.Column2, Total: g.Sum(o => o.Quantity));
or you can write linq group by multiple columns to list in c# as shown below.
var result =
from o in db
group o by (o.Column1, o.Column2) into g
select new {
Column1 = g.Key.Column1,
Column2 = g.Key.Column2,
Total = g.Sum(o => o.Quantity)
}
if you want to write c# linq group by multiple columns count, you can modify as shown below.
var result =
from o in db
group o by (o.Column1, o.Column2) into g
select new {
Column1 = g.Key.Column1,
Column2 = g.Key.Column2,
Total = g.Count()
}
or you can use method
var result = db.GroupBy(x => (x.Column1, x.Column2))
.Select(g => (g.Key.Column1, g.Key.Column2, Total: g.Sum(x => x.Quantity)));