c# - 在 LINQ 中按时间跨度分组

标签 c# linq group-by

假设我们有这样一个类:

    class Forecast
    {
      int LocationId;
      int DepartmentId;
      DateTime StartDate;
      DateTime EndDate;
      int CountOfEmployees;
    }

我有一个预测列表:List<Forecasts> forecasts

列表按 15 分钟的间隔分组,例如:

forecast[0] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 06:00:00.000, EndTime = 2018-10-01 06:15:00.000, CountOfEmployees = 2  }
forecast[1] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 06:15:00.000, EndTime = 2018-10-01 06:30:00.000, CountOfEmployees = 1  }
forecast[2] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 06:30:00.000, EndTime = 2018-10-01 06:45:00.000, CountOfEmployees = 3  }
forecast[3] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 06:45:00.000, EndTime = 2018-10-01 07:00:00.000, CountOfEmployees = 1  }
forecast[4] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 07:00:00.000, EndTime = 2018-10-01 07:15:00.000, CountOfEmployees = 2  }
forecast[5] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 07:15:00.000, EndTime = 2018-10-01 07:30:00.000, CountOfEmployees = 2  }
forecast[6] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 07:30:00.000, EndTime = 2018-10-01 07:45:00.000, CountOfEmployees = 5 }
forecast[7] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 07:45:00.000, EndTime = 2018-10-01 08:00:00.000, CountOfEmployees = 3  }
forecast[8] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 06:00:00.000, EndTime = 2018-10-01 06:15:00.000, CountOfEmployees = 2  }
forecast[9] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 06:15:00.000, EndTime = 2018-10-01 06:30:00.000, CountOfEmployees = 1  }
forecast[10] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 06:30:00.000, EndTime = 2018-10-01 06:45:00.000, CountOfEmployees = 3  }
forecast[11] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 06:45:00.000, EndTime = 2018-10-01 07:00:00.000, CountOfEmployees = 1  }
forecast[12] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 07:00:00.000, EndTime = 2018-10-01 07:15:00.000, CountOfEmployees = 2  }
forecast[13] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 07:15:00.000, EndTime = 2018-10-01 07:30:00.000, CountOfEmployees = 2  }
forecast[14] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 07:30:00.000, EndTime = 2018-10-01 07:45:00.000, CountOfEmployees = 5 }
forecast[15] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 07:45:00.000, EndTime = 2018-10-01 08:00:00.000, CountOfEmployees = 3  }

我希望结果按 60 分钟的时间间隔、位置和部门以及 countOfEmployees 的总和进行分组。所以预期的结果应该是这样的:

result[0] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 06:00:00.000, EndTime = 2018-10-01 07:00:00.000, CountOfEmployees = 7  }
result[1] = new Forecast { LocationId = 1, DepartmentId = 1, StartTime = 2018-10-01 07:00:00.000, EndTime = 2018-10-01 08:00:00.000, CountOfEmployees = 12  }
result[2] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 07:00:00.000, EndTime = 2018-10-01 08:00:00.000, CountOfEmployees = 12  }
result[3] = new Forecast { LocationId = 2, DepartmentId = 2, StartTime = 2018-10-01 07:00:00.000, EndTime = 2018-10-01 08:00:00.000, CountOfEmployees = 12  }

有人可以指出我正确的方向吗?

最佳答案

您可以尝试将 linq selectgroup by 一起使用

在 linq GroupBy 中为您的 EndTimeStartTimeLocationId 逻辑建立时间分组>

每小时的

StartTimeEndTime

var result = forecast
.GroupBy(_ => new {
    _.LocationId,
    StartTime = new DateTime(_.StartTime.Year,
                              _.StartTime.Month,
                              _.StartTime.Day,
                              _.StartTime.Hour,0,0,0),
    EndTime = new DateTime(_.StartTime.Year,
                              _.StartTime.Month,
                              _.StartTime.Day,
                              _.StartTime.Hour + 1, 0, 0, 0)
})
.Select(x => new {
    x.Key.LocationId,
    x.Key.StartTime,
    x.Key.EndTime,
    CountOfEmployees = x.Sum(y=>y.CountOfEmployees)});

c# online

结果

LocationId : 1 StartTime : 10/1/2018 6:00:00 AM EndTime : 10/1/2018 7:00:00 AM  CountOfEmployees : 7
LocationId : 1 StartTime : 10/1/2018 7:00:00 AM EndTime : 10/1/2018 8:00:00 AM  CountOfEmployees : 12
LocationId : 2 StartTime : 10/1/2018 6:00:00 AM EndTime : 10/1/2018 7:00:00 AM  CountOfEmployees : 7
LocationId : 2 StartTime : 10/1/2018 7:00:00 AM EndTime : 10/1/2018 8:00:00 AM  CountOfEmployees : 12

关于c# - 在 LINQ 中按时间跨度分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53512385/

相关文章:

postgresql - 我的 PostgreSQL 语句有什么问题?这在 MySql 上运行

c# - SQLAzureExecutionStrategy ShouldRetryOn 未在 EF 异常上调用

c# - 将选中的 Form1.checkBoxes 导出到 Form2.listBox

c# - IEnumerable<T> 到 CSV 文件

c# - Linq 投影在 NHibernate 3.2 中被错误缓存

mysql - groupBy select 后计算总记录数

c# - 如何在 C# 中定义和调用 api Post

c# - 尝试使用 WebRequest 调用 WCF 服务

c# - LINQ:为什么要使用 ...Take(1).SingleOrDefault()?

mysql - 如何强制MySQL "Using index for group-by"