Counting Using Group By Linq

后端 未结 2 962
爱一瞬间的悲伤
爱一瞬间的悲伤 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.

提交回复
热议问题