订购日期列表进行比较

问题描述 投票:2回答:4

我以前在这里问这个问题:List of Dates ordered in a certain way

我想提出的解决方案是罚款,直到今年选中了我的约会对象名单上,遇到的问题。

我的最新名单(在此字符串基于格式 - 这是怎样的数据从源API到我这里来)

201711
201712
201801
201811
201812
201901

我想提出我的数据的条形图显示时3个月年对月才能年度比较。这将意味着我命令列表,所以

201711
201811
201712
201812
201801
201901

所以,我可以再看看在为了一年同期棒材十一月,十二月和一月

我试图在这个问题底部的解决方案,但它的地方,像这样的顺序(这是不是我想要的):

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);
c# arrays list linq datetime
4个回答
7
投票

您可以使用此查询的办法,首先判断一个月的群体:

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);

2
投票

我可以用管理来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));
}

1
投票

在我看来,这是足够的:

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

有根本没有必要淤泥与周围解析日期。这是一个简单的字符串排序和分组这种方式。


0
投票

所以,你被卡住对象的序列,其中每个对象都有字符串类型的格式YYYYMM一个Date属性。并希望从中提取一些数据。

您的日期格式是独立的语言。不要紧,你的电脑是否是英国的一个或一个中国。 date属性总是会在格式YYYYMM。

这使得它很容易将它转换成一个DateTime格式,这使得它易于访问的年份和月份。

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 /月,一个NrOfYears并且要分组dateTimeItems到当月组的方式NrOfMonths。

例如,起始于2018-11,我想4个月组,连续三年(是啊,是啊,我知道你原来的要求是只有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 = ...

我们将与同月项目组。我们不希望今年所有月份,我们只需要几个月。如果我们希望3个月内开始在第11个月,我们需要保持组,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
© www.soinside.com 2019 - 2024. All rights reserved.