在LINQ中按时间范围分组

时间:2018-11-28 04:54:33

标签: 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  }

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

4 个答案:

答案 0 :(得分:3)

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

为您的GroupByEndTimeStartTime的逻辑在linq LocationId中建立时间分组

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

答案 1 :(得分:1)

您可以做类似的事情,假设您的间隔总是从00分钟到60分钟开始,而不是从6:15到7:15。

var result = forecast.GroupBy(x=>new {x.StartTime.Hour, x.LocationId, x.DepartmentId})
                    .Select(x=> new Forecast
                                { 
                                    LocationId = x.Key.LocationId, 
                                    DepartmentId = x.Key.DepartmentId, 
                                    StartTime =  x.ToList().OrderBy(e=>e.StartTime).First().StartTime,
                                    EndTime =  x.ToList().OrderBy(e=>e.EndTime).Last().EndTime,
                                    CountOfEmployees = x.ToList().Sum(c=>c.CountOfEmployees) 
                                    });

答案 2 :(得分:0)

这应该有效。正如@SeM所述,您需要展示您的研究。您的问题没有任何帮助。

var results = from foreCastSelector in foreCasts
          group foreCastSelector by new
          {
              LocationId = foreCastSelector.LocationId,
              DepartmentId = foreCastSelector.DepartmentId,
              StartDate = foreCastSelector.StartDate.AddMinutes(-1 * foreCastSelector.StartDate.Minute),
              EndDate = foreCastSelector.StartDate.AddMinutes(-1 * foreCastSelector.StartDate.Minute).AddHours(1)
          } into gcs
          select new Forecast
          {
              LocationId = gcs.Key.LocationId,
              DepartmentId = gcs.Key.DepartmentId,
              StartDate = gcs.Key.StartDate,
              EndDate = gcs.Key.EndDate,
              CountOfEmployees = gcs.Sum(r => r.CountOfEmployees)
              };

foreach (var result in results)
{
    Console.WriteLine(result.LocationId + " | " + result.DepartmentId + " | "
        + result.StartDate.ToString() + " | "
       + result.EndDate.ToString() + " | "
       + result.CountOfEmployees);
}

答案 3 :(得分:0)

您可以删除时间,然后按新的StartTime分组:

var result = forecast
.GroupBy(g => new { StartTime = g.StartTime.Date.AddHours(g.StartTime.Hour), g.LocationId, g.DepartmentId })
.Select(s => 
    new Forecast()
    {
        LocationId = s.Key.LocationId,
        DepartmentId = s.Key.DepartmentId,
        StartTime = s.Key.StartTime,
        EndTime = s.Max(m => m.EndTime),
        CountOfEmployees = s.Sum(c => c.CountOfEmployees),
    }
)
.OrderBy(o => o.LocationId)
.ThenBy(o => o.DepartmentId)
.ThenBy(o => o.StartTime)
.ToList();

或使用查询语法:

var q = from f in forecast
        group f by new 
        { 
            StartTime = f.StartTime.Date.AddHours(f.StartTime.Hour), 
            f.LocationId, 
            f.DepartmentId 
        } into g
        orderby g.Key.LocationId, g.Key.DepartmentId, g.Key.StartTime
        select new Forecast
        {
            LocationId = g.Key.LocationId,
            DepartmentId = g.Key.DepartmentId,
            StartTime = g.Key.StartTime,
            EndTime = g.Max(m => m.EndTime),
            CountOfEmployees = g.Sum(s => s.CountOfEmployees)
        };

输出:

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

DotNetFiddle Example