簡體   English   中英

訂單日期列表進行比較

[英]Order date list for comparison

我之前在這里問過這個問題: 以某種方式訂購的日期列表

我認為建議的解決方案沒問題, 直到我的日期列表上的年份已經過去並遇到問題。

我的日期列表(以這種基於字符串的格式 - 這是數據來自源API的方式)

201711
201712
201801
201811
201812
201901

我想在條形圖中顯示我的數據, 以按月順序顯示3個月的年度比較 這意味着我按順序排列列表

201711
201811
201712
201812
201801
201901

所以我可以按順序查看11月,12月和1月的年度酒吧。

我已經在問題的底部嘗試了解決方案,但它按順序排列順序(這不是我想要的):

201801
201901
201711
201811
201712
201812

為清楚起見,下個月它需要繼續作為這個日期列表:

我想要的第一個月總是比現在的那個月早2個月

201712
201812
201801
201901
201802
201902

var rowsInOrder = dateList.OrderBy(n => DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Month).ThenBy(n=> DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Year);

您可以使用首先確定月份組的查找方法:

var monthLookup = dateList
    .Select(s => new{String = s, Date = DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture)})
    .OrderBy(x => x.Date)  // not necessary in your sample data but i assume it's desired
    .ToLookup(x=> x.Date.Month);
var rowsInOrder = monthLookup.SelectMany(x => x).Select(x => x.String);

我可以設法使用GroupBy來實現您的目標,

var rowsInOrder = new List<string>();
foreach (var grouping in dates.GroupBy(s =>
        DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month))
{
    rowsInOrder.AddRange(grouping.OrderBy(s => s));
};

您也可以使用相同的邏輯訂購月份:

var rowsInOrder = new List<string>();
foreach (var grouping in dates
    .OrderBy(s => DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month).GroupBy(s =>
        DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month))
{
    rowsInOrder.AddRange(grouping.OrderBy(s => s));
}

在我看來,這已足夠:

var rowsInOrder = dates.OrderBy(x => x).GroupBy(x => x.Substring(4)).SelectMany(x => x);

解析日期根本不需要搗亂。 這是一個簡單的字符串排序和分組方式。

因此,您會遇到一系列對象,其中每個對象都具有格式為yyyyMM的字符串類型的Date屬性。 並且您想從中提取一些數據。

您的日期格式與語言無關。 您的計算機是英國計算機還是中國計算機並不重要。 Date屬性將始終采用yyyyMM格式。

這使得將其轉換為DateTime格式變得相當容易,這使得訪問Year和Month變得容易。

const string dateTimeFormat = "yyyyMM";
CultureInfo provider = CultureInfo.InvariantCulture;
var dateList = ...      // your original list of items with the string Date property

var itemsWithYearMonth = dateList.Select(item => new
{
    DateTime = DateTime.ParseExact(item, dateTimeFormat, provider)
    ... // select other items you need for your bar chart
});

現在,給定一個StartYear / Month,一個NrOfYears和一個NrOfMonths,你想將dateTimeItems分組到同月的組中。

例如,從2018年11月開始,我想要連續三年四個月的團體(是的,是的,我知道你原來的要求只有3個月,2年,但為什么要限制自己,讓我們做你的代碼可重用):

group 1: 2018-11, 2019-11, 2020-11, 2021-11
group 2: 2018-12, 2019-12, 2020-12, 2021-12
group 3: 2019-01, 2020-01, 2021-01, 2022-01

獎勵積分:我們將通過年度邊界!

所以輸入:

var itemsWithYearMonth = ... // see above
int startYear = ...
int startMonth = ...
int nrOfMonths = ...    
int nrOfYears = ...

我們將用相同的月份制作一組物品。 我們不想要一年中的所有月份,我們只想要幾個月。 如果我們想要從第11個月開始的3個月,我們需要將這些組保持為11,12,1個月。

var desiredMonths = Enumerable.Range(startMonth, nrOfMonths)   // example: 11, 12, 13
    .Select(monthNr => 1 + ((monthNr-1) % 12));                // 11, 12, 1

根據您的輸入,我們不希望所有月份,我們只希望年/月大於起始月份。

DateTime startMonth = new DateTime(startYear, startMonth, 1);

最簡單的方法是僅保留日期等於或大於startMonth的項目的輸入源,並僅采用每個組的第一個NumberOfYears項目。 這樣,如果您像我在示例中所做的那樣傳遞年份邊界,則可以獲得正確數量的項目。

var result = itemsWithYearMonth
    // keep only the items newer than startMonth
    .Where(item => item.DateTime >= startMonth)

    // group by same month:
    .GroupBy(item => item.DateTime.Month,
    (month, itemsWithThisMonth) => new
    {
        Month = month,        // in my example: 11, 12, 1, ...

        // in every group: take the first nrOfYears items:
        Items = itemsWithThisMonth
                // order by ascending year
                .OrderBy(itemWithThisMonth => itemWithThisMonth.Year)
                // no need to order by Month, all Months are equal in this group
                .Take(nrOfYears)
                .ToList(),
     })
     // keep only the desired months:
     .Select(group => desiredMonth.Contains(group.Month));

所以現在你有了團體:

group of month 11, with data of years 2018, 2019, 2020, 2021
group of month 12, with data of years 2018, 2019, 2020, 2021
group of month 01, with data of years 2019, 2020, 2021, 2022

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM