开发者

Linq group by and count - how do I add more columns?

开发者 https://www.devze.com 2022-12-14 19:25 出处:网络
I have the following linq query from c in AllContracts group c.StatusId by c.StatusDescription into g select new {StatusDescription = g.Key, CountOf = g.Count()}

I have the following linq query

from c in AllContracts
group c.StatusId by c.StatusDescription
into g
select new {StatusDescription = g.Key, CountOf = g.Count()}

which returns

StatusDescription       CountOf
End date has passed     1
Suspended               1
Setup phase             2
Running         开发者_JAVA技巧        7

However I would love to add the column StatusId into the results and also order by StatusId. I have googled around but I am stumped. Can anyone help?


Try the following:

(from c in AllContracts
group c by new {c.StatusId, c.StatusDescription}
into g
select new {
    StatusId = g.Key.StatusId,
    StatusDescription = g.Key.StatusDescription,
    CountOf = g.Count()
}).OrderBy(item => item.StatusId)
0

精彩评论

暂无评论...
验证码 换一张
取 消