从datetime对象的集合解析出月度项目 [英] Parse out monthly items from a collection of DateTime objects

查看:125
本文介绍了从datetime对象的集合解析出月度项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的问这个问题通过星期几和时间

I previously asked this question to take a oollection of datetime objects and group them by dayOfweek and time

所以只是为了回顾采取datetime对象和他们组的oollection:我的DateTime <集合/ p>

So just to recap: I take a collection of DateTime

List<DateTime> collectionOfDateTime = GetDateColletion();



,然后通过DAYOFWEEK分组和时间做这个。

and then grouping by dayofWeek and time of day by doing this

var byDayOfWeek = collectionOfDateTime.GroupBy(dt => dt.DayOfWeek + "-" + dt.Hour + "-" + dt.Minute);



所以在这一点上,我有一周一致(时间)完美的工作,这些分组。

So at this point, I have these grouped by week (consistent time) working perfectly.

我现在要组一个新的要求按月而不是按周。当我说月,它不是一个月,但像

I now have a new requirement to group by Month instead of by week. When i say "month", its not the same day of the month but something like "the first tuesday of each Month"

我试图找出什么钥匙每个月的第一个星期二的东西的当天一组中使用由分组适合的月度逻辑(该月的第一个星期二,每个月的第二个星期五,等等)

I am trying to figure out what "key" to use in a group by to group all items that fit that monthly logic (the first tuesday of the month, the second friday of each month, etc)

作为所有项目例如,假设我有这些日期开始了用;

As an example lets say i had these dates to start out with;

var date1 = new DateTime(2013, 1, 4).AddHours(8);  // This is the first Friday in Jan
var date2 = new DateTime(2013, 2, 1).AddHours(8);  // This is the first Friday in Feb
var date3 = new DateTime(2013, 1, 5).AddHours(3);  // This is the first Sat in Jan
var date4 = new DateTime(2013, 2, 2).AddHours(3);  // This is the first Sat in Feb
var date5 = new DateTime(2013, 2, 2).AddHours(6);  // This is the first Sat in Feb - different time

如果这些是走进日期原数组,我需要一个GROUPBY 3组结束。

If these were the dates that went into the original array, i need a groupby to end up with 3 groups.


  • 第一组将有日期1&放大器; DATE2在它

  • 第二组将有DATE3和date4在里面。

  • date5将是对自己的,因为它不符合任何其他组给出的不同时间

任何人都可以通过标准反正建议组

Can anyone suggest anyway to group by that criteria?

推荐答案

我觉得它更容易比它看起来:

I think it's easier than it looks:

var byDayOfMonth = from d in dates
                   let h = (d.Day / 7) + 1
                   group d by new { d.DayOfWeek, h } into g
                   select g;



局部变量 H =(d.Day / 7)+ 1 其中星期几当月它实际上是在

Local variable h = (d.Day / 7) + 1 sets which DayOfWeek within that month it actually is.

我运行测试,并获得集2组,完全一样在你的例子。该组键是:

I run it for test and received 2 groups, exactly the same as in your example. Keys for that groups are:

{ DayOfWeek = Friday, h = 1 }
{ DayOfWeek = Saturday, h = 1 }

什么手段,也有群体的本月的第一个星期五的和的本月的第一个周六的'。

What means, there are groups for 'First Friday of month' and 'First Saturday of month'.

您可以轻松地扩展通过组合键 d.Hour 和/或 d.Minute 如果你喜欢:

You can easily extend grouping key by d.Hour and/or d.Minute if you like:

var byDayOfMonth = from d in dates
                   let h = (d.Day / 7) + 1
                   group d by new { d.DayOfWeek, h, d.Hour, d.Minute } into g
                   select g;



结果(键只):

Results (keys only):

{ DayOfWeek = Friday, h = 1, Hour = 8, Minute = 0 }
{ DayOfWeek = Saturday, h = 1, Hour = 3, Minute = 0 }
{ DayOfWeek = Saturday, h = 1, Hour = 6, Minute = 0 }

这篇关于从datetime对象的集合解析出月度项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆