Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ counting elements with DISTINCT

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
like image 889
Ras Avatar asked Jul 15 '13 10:07

Ras


1 Answers

use group by

var dateGrouped = dates.GroupBy(x => x.Key)
                       .Select(x => new { Date = x.Key, Values = x.Distinct().Count() });
like image 61
gzaxx Avatar answered Sep 21 '22 06:09

gzaxx