Take a look at example:
var data = new[] {
new {listed_companyid = 2, numberof_units= 4, userid =2},
new {listed_companyid = 2, numberof_units= 2, userid =2},
new {listed_companyid = 1, numberof_units= 6, userid =2},
new {listed_companyid = 5, numberof_units= 3, userid =3}
};
var result = data.GroupBy(x=>x.userid)
.Select(grp=> new
{
userid = grp.Key,
totalunits = grp.Sum(x=>x.numberof_units)
})
.ToList();
result.Dump();
var result2 = data.GroupBy(x=>x.userid)
.Select(grp=> new
{
userid = grp.Key,
details = grp.GroupBy(x=>x.listed_companyid)
.Select(subgrp=> new
{
companyid = subgrp.Key,
units = subgrp.Sum(y=>y.numberof_units)
})
.ToList()
})
.ToList();
result2.Dump();
Result:
userid totalunits
2 12
3 3
Result#2
userid | details |
---|
companyid | units |
---|
2 | 2 | 6 |
1 | 6 |
3 | 5 | 3 |
Good luck!