具有DISTINCT的C#LINQ计数元素 [英] C# LINQ counting elements with DISTINCT
本文介绍了具有DISTINCT的C#LINQ计数元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个KeyValuePair项目集合,其中DateTime为键,字符串为值. 基本上我的数据如下:
I have a collection of KeyValuePair items with DateTime as key and a string as value. Basically my data looks like:
12/07/2013 - 10220
12/07/2013 - 10220
12/07/2013 - 12220
12/07/2013 - 11240
12/07/2013 - 15220
13/07/2013 - 23220
13/07/2013 - 35620
14/07/2013 - 15620
14/07/2013 - 15620
我希望有一份清单,列出我每天有多少件(不同的)物品.因此查询将导致:
I would like to have a List of how many items (distinct) I've for each days. So the query would result in a:
12/07/2013 - 4
13/07/2013 - 2
14/07/2013 - 1
推荐答案
使用group by
var dateGrouped = dates.GroupBy(x => x.Key)
.Select(x => new { Date = x.Key, Values = x.Distinct().Count() });
这篇关于具有DISTINCT的C#LINQ计数元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文