我想将我的DateRange列表中的持续时间按周或季度进行汇总。
我不确定这是最好的方法。
List<Event> events = new List<Event>();
class Event
{
public string EventName {get;set;}
public DateTime date {get;set;}
public double duration {get; set;}
}我使用的是LingBridge库,它允许.Net-2.0中的λ表达式
发布于 2011-05-26 22:26:48
您将需要使用for或foreach或类似的方法迭代您的集合。
例如,对于Q1:
List<Event> Q1Events = new List<Event>();
foreach (Event e in events)
{
if (e.date.Month >= 1 && e.date.Month <= 3)
Q1Events.Add(e);
}发布于 2011-05-26 22:32:26
下面是一些2.0兼容的代码来实现日期分组。您可以调整它以对类的DateTime属性进行分组。
List<DateTime> dates = ...
Dictionary<int, IList<DateTime>> groupedDates
= new Dictionary<int, IList<DateTime>>();
foreach (DateTime date in dates)
{
int quarter = (date.Month / 3) + 1;
if (groupedDates.ContainsKey(quarter))
{
groupedDates[quarter].Add(date);
}
else
{
List<DateTime> dateGroup = new List<DateTime>();
dateGroup.Add(date);
groupedDates.Add(quarter, dateGroup);
}
}发布于 2011-05-26 22:23:32
这将按一年中的某一天对其进行分组:
events.GroupBy(e => string.Format("{0}.{1}+", e.date.DayOfYear, e.date.Year);因此,现在您只需找出日期的WeekOfYear或QuarterOfYear属性,并将其用作分组子句。
对于QuarterOfYear,可能如下所示:
events.GroupBy(e => string.Format("{0}.{1}+", (e.date.Month % 4) + 1, e.date.Year);但对于这一周来说,事情变得更加复杂。在我的记忆中,有不同的方法开始计算一年中的周数。检查NodaTime或其他一些数据库为您做这件事...
https://stackoverflow.com/questions/6140094
复制相似问题