Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#LINQ计数元素具有不同的_C#_Linq_Distinct_Key Pair - Fatal编程技术网

C#LINQ计数元素具有不同的

C#LINQ计数元素具有不同的,c#,linq,distinct,key-pair,C#,Linq,Distinct,Key Pair,我有一组KeyValuePair项,其中DateTime为键,字符串为值。 基本上,我的数据如下所示: 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 我想有一个清单,列出我每天有多少项目(不同)。因此,

我有一组KeyValuePair项,其中DateTime为键,字符串为值。 基本上,我的数据如下所示:

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
我想有一个清单,列出我每天有多少项目(不同)。因此,查询将导致:

12/07/2013 - 4
13/07/2013 - 2
14/07/2013 - 1

使用
分组依据

var dateGrouped = dates.GroupBy(x => x.Key)
                       .Select(x => new { Date = x.Key, Values = x.Distinct().Count() });

下面是完整示例的解决方案

        var list = new[]
        {
            new {Date = new DateTime(2013,07,12), value = 10220},
            new {Date = new DateTime(2013,07,12), value = 10220},
            new {Date = new DateTime(2013,07,12), value = 12220},
            new {Date = new DateTime(2013,07,12), value = 11240},
            new {Date = new DateTime(2013,07,12), value = 15220},
            new {Date = new DateTime(2013,07,13), value = 23220},
            new {Date = new DateTime(2013,07,13), value = 35620},
            new {Date = new DateTime(2013,07,14), value = 15620},
            new {Date = new DateTime(2013,07,14), value = 15620},
        };


        var res = from l in list group l by new { l.Date } into g select new { g.Key.Date, value = g.Distinct().Count() };

        foreach (var item in res)
        {
            Console.WriteLine(item.Date + " " + item.value);
        }