Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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# C使用Linq统计每个日期的发生率_C#_Linq - Fatal编程技术网

C# C使用Linq统计每个日期的发生率

C# C使用Linq统计每个日期的发生率,c#,linq,C#,Linq,我有这样一个对象列表: public class Entity { public int Id { get; set; } public DateTime Date { get; set; } } | Id | Date | | 1 | 2017-01-16 12:58:32 | | 2 | 2017-01-16 11:36:01 | | 3 | 2017-01-16 17:55:19 | | 4 | 2017-01-19 13:19:4

我有这样一个对象列表:

public class Entity
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
}

| Id | Date                | 
| 1  | 2017-01-16 12:58:32 |
| 2  | 2017-01-16 11:36:01 |
| 3  | 2017-01-16 17:55:19 |
| 4  | 2017-01-19 13:19:40 |
| 5  | 2017-01-19 09:21:55 |
| Date       | Occurrences |
| 2017-01-16 |   3         | 
| 2017-01-17 |   2         | 
我想用LINQ来过滤这些数据,以计算每天的眼病数量。结果是这样的:

public class Entity
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
}

| Id | Date                | 
| 1  | 2017-01-16 12:58:32 |
| 2  | 2017-01-16 11:36:01 |
| 3  | 2017-01-16 17:55:19 |
| 4  | 2017-01-19 13:19:40 |
| 5  | 2017-01-19 09:21:55 |
| Date       | Occurrences |
| 2017-01-16 |   3         | 
| 2017-01-17 |   2         | 
是否可以使用LINQ执行此操作?

是否要使用GroupBy


试试这样的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Xml;
using System.Xml.Linq;



namespace ConsoleApplication49
{
    class Program
    {

        static void Main(string[] args)
        {
            List<Entity> entities = new List<Entity>() {
                new Entity() { Id = 1, Date = DateTime.Parse("2017-01-16 12:58:32")},
                new Entity() { Id = 2, Date = DateTime.Parse("2017-01-16 11:36:01")},
                new Entity() { Id = 3, Date = DateTime.Parse("2017-01-16 17:55:19")},
                new Entity() { Id = 4, Date = DateTime.Parse("2017-01-19 13:19:40")},
                new Entity() { Id = 5, Date = DateTime.Parse("2017-01-19 09:21:55")}
            };

            var results = entities.GroupBy(x => x.Date.Date).Select(x => new { count = x.Count(), entities = x.ToList() }).ToList();
        }


    }
    public class Entity
    {
        public int Id { get; set; }
        public DateTime Date { get; set; }
    }



}

以下是两种解决方案:

1.第一种方法使用LINQ查询实体。 2.第二种方法使用多个LINQ函数对实体进行分组和计数。 你想要GroupBy