久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

分組連續日期

Grouping Contiguous Dates(分組連續日期)
本文介紹了分組連續日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 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 是序列的計數.雖然通常同時使用 FirstCount 會迭代源序列兩次,但我們碰巧知道 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 国产精品久久久久久av公交车 | 婷婷综合五月天 | 国产精品久久久久久中文字 | 国产精品夜间视频香蕉 | 欧美精品日韩精品 | 久久久国产一区二区三区 | 亚洲欧美中文日韩在线v日本 | 亚洲国产一区二区三区在线观看 | 日本不卡一区二区三区在线观看 | 精品国产综合 | 在线国产一区二区 | 日本淫视频 | 欧美精品片 | 男人的天堂一级片 | 日韩一级免费电影 | 日韩精品一区二区三区中文在线 | 日本免费一区二区三区四区 | 久久婷婷av| 成人免费毛片在线观看 | 国产精品亚洲欧美日韩一区在线 | 久久精品影视 | 一区二区三区播放 | 福利网址| 国产精品成人一区二区三区 | 在线观看成人av | 欧美综合久久久 | 欧美视频一区二区三区 | 欧美一级片中文字幕 | 看毛片网站 | 91在线电影 | 天堂亚洲 | 亚洲一区二区三区在线 | 国产蜜臀97一区二区三区 | 伊人超碰在线 | 99福利网| 亚洲精品一区二区三区在线观看 | 男女网站视频 | av电影一区 | 久久久免费观看视频 | 国产成人在线观看免费 | www.亚洲免费 |