Counting Using Group By Linq

后端 未结 2 963
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 02:02

I have an object that looks like this:

Notice 
{
    string Name,
    string Address 
}

In a List I want to output A

相关标签:
2条回答
  • 2021-02-05 02:50

    A variation on Leniel's answer, using a different overload of GroupBy:

    var query = notices.GroupBy(n => n.Name, 
                    (key, values) => new { Notice = key, Count = values.Count() });
    

    Basically it just elides the Select call.

    0 讨论(0)
  • 2021-02-05 03:06
    var noticesGrouped = notices.GroupBy(n => n.Name).
                         Select(group =>
                             new
                             {
                                 NoticeName = group.Key,
                                 Notices = group.ToList(),
                                 Count = group.Count()
                             });
    
    0 讨论(0)
提交回复
热议问题