GroupBy and count the unique elements in a List

后端 未结 4 1638
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 09:56

I have a list that contains only strings. What I would love to do is group by and return a count.

For instance:

Foo1
Foo2
Foo3
Foo1
Foo2 
Foo2
         


        
4条回答
  •  隐瞒了意图╮
    2020-12-17 10:25

     var items= myList
        .GroupBy(g => g)
        .Select(t => new {count= t.Count(), key= t.Key });
    
     foreach (var group in items)
         Console.WriteLine ( group.key + " " + group.count);
    

提交回复
热议问题