C# LINQ counting elements with DISTINCT

后端 未结 2 1928
忘掉有多难
忘掉有多难 2020-12-19 07:11

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/         


        
2条回答
  •  醉梦人生
    2020-12-19 07:46

    Below is the solution with complete example

            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);
            }
    

提交回复
热议问题