問題描述
我有一個 List
我有一個課程:
class NonWorkingDay
{
public DateTime Start;
public int Days;
}
我正在嘗試找出一種干凈的方法來將它們分組.
I am trying to figure out a clean way to group them.
public List<NonWorkingDay> GetContiguousDates(List<DateTime> dates)
{
}
注意:如果周五有 NWD,下周一是周一,則應將其分組.周末不考慮.
Note: if there is an NWD on Friday and the next is Monday they should be grouped. Weekends are not considered.
例如如果我有
September 3 2013
September 20 2013
September 23 2013
September 24 2013
September 30 2013
October 1 2013
輸出將是:
Start = September 3 2013, Days = 1
Start = September 20 2013, Days = 3 //weekend got skipped
Start = September 30 2013, Days = 2
有沒有辦法做到這一點(沒有一堆計數器變量)并使用 .Select 或 .Where 之類的.
Is there any way to do this (without have a bunch of counter variables) and using .Select or .Where or something.
謝謝
推薦答案
所以,我們將從這個通用迭代器函數開始.它需要一個序列和一個接受兩個項目并返回一個布爾值的謂詞.它將從源中讀取項目,而一個項目連同它的前一個項目,根據謂詞返回 true,下一個項目將在下一組"中.如果返回false,則前一組已滿,下一組開始.
So, we'll start out with this generic iterator function. It takes a sequence and a predicate that accepts two items and returns a boolean. It will read in items from the source and while an item, along with it's previous item, returns true based on the predicate, that next item will be in the "next group". If it returns false, the previous group is full and the next group is started.
public static IEnumerable<IEnumerable<T>> GroupWhile<T>(this IEnumerable<T> source
, Func<T, T, bool> predicate)
{
using (var iterator = source.GetEnumerator())
{
if (!iterator.MoveNext())
yield break;
List<T> currentGroup = new List<T>() { iterator.Current };
while (iterator.MoveNext())
{
if (predicate(currentGroup.Last(), iterator.Current))
currentGroup.Add(iterator.Current);
else
{
yield return currentGroup;
currentGroup = new List<T>() { iterator.Current };
}
}
yield return currentGroup;
}
}
我們還需要這個簡單的輔助方法,它根據日期獲取下一個工作日.如果您還想將假期納入其中,那么它就會從瑣碎到非常困難,但這就是邏輯所在.
We'll also need this simple helper method that gets the next working day based on a date. If you want to incorporate holidays as well it goes from trivial to quite hard, but that's where the logic would go.
public static DateTime GetNextWorkDay(DateTime date)
{
DateTime next = date.AddDays(1);
if (next.DayOfWeek == DayOfWeek.Saturday)
return next.AddDays(2);
else if (next.DayOfWeek == DayOfWeek.Sunday)
return next.AddDays(1);
else
return next;
}
現在把它們放在一起.首先我們訂購天數.(如果您確保它們總是按順序排列,您可以刪除該部分.)然后我們將連續的項目分組,而每個項目都是前一個工作日的下一個工作日.
Now to put it all together. First we order the days. (If you ensure they always come in ordered you can remove that part.) Then we group the consecutive items while each item is the next work day of the previous.
然后我們需要做的就是將連續日期的 IEnumerable
轉換為 NonWorkingDay
.因為開始日期是第一個日期,Days
是序列的計數.雖然通常同時使用 First
和 Count
會迭代源序列兩次,但我們碰巧知道 GroupWhile
返回的序列實際上是一個 List
在引擎蓋下,因此多次迭代它不是問題,獲得 Count
甚至是 O(1).
Then all we need to do is turn an IEnumerable<DateTime>
of consecutive dates into a NonWorkingDay
. For that the start date is the first date, and Days
is the count of the sequence. While normally using both First
and Count
would iterate the source sequence twice, we happen to know that the sequence returned by GroupWhile
is actually a List
under the hood, so iterating it multiple times is not a problem, and getting the Count
is even O(1).
public IEnumerable<NonWorkingDay> GetContiguousDates(IEnumerable<DateTime> dates)
{
return dates.OrderBy(d => d)
.GroupWhile((previous, next) => GetNextWorkDay(previous).Date == next.Date)
.Select(group => new NonWorkingDay
{
Start = group.First(),
Days = group.Count(),
});
}
這篇關于分組連續日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!