从日期集合中查找日期范围C# [英] Find date ranges from a collection of dates C#

查看:57
本文介绍了从日期集合中查找日期范围C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题.我有日期的有序集合.他们是英国约会

Simple question. I have an ordered collection of dates. They are UK dates btw

01/01/10
01/02/10
01/03/10
01/04/10
02/04/10
03/04/10
04/04/10

我想将其转换为日期范围的集合

And I want to convert this into a collection of date ranges

01/01/10 -> 01/01/10
01/02/10 -> 01/02/10
01/03/10 -> 01/03/10
01/04/10 -> 04/04/10

请澄清一下,我正在尝试将任何连续的日期转换为一个范围.因此前3个日期是单独的,后4个日期转换为4月1日至4月4日的范围.

Just to clarify, I'm trying to convert any consecutive dates into a range. so the first 3 dates are stand alone and the last 4 get converted into a range 1st of April to 4th of April.

现在我可以使用循环来做到这一点,但这不是很优雅.有没有人提供解决方案?

Now I can do this using loops but it's not very elegant. Does any one have any solutions out there that are?

谢谢

推荐答案

鉴于您要确定连续日期范围的范围,我认为您唯一的选择就是循环.不过,您可以一次完成此操作,然后将其放入扩展方法中,这样它就可以在任何 IList< DateTime> 上运行,例如:

Given that you want to determine ranges of consecutive date ranges, I think your only option is, as you say a loop. You can do it in a single pass though, and put it in an extension method so it'll operate on any IList<DateTime>, for example:

// purely an example, chances are this will have actual, y'know logic in live
public class DateRange
{
    private List<DateTime> dates = new List<DateTime>();

    public void Add(DateTime date)
    {
        this.dates.Add(date);
    }

    public IEnumerable<DateTime> Dates
    {
        get { return this.dates; }
    }
}

public static IEnumerable<DateRange> GetRanges(this IList<DateTime> dates)
{
    List<DateRange> ranges = new List<DateRange>();
    DateRange currentRange = null;

    // this presumes a list of dates ordered by day, if not then the list will need sorting first
    for( int i = 0; i < dates.Count; ++i )
    {
        var currentDate = dates[i];
        if( i == 0 || dates[i - 1] != currentDate.AddDays(-1))
        {
            // it's either the first date or the current date isn't consecutive to the previous so a new range is needed
            currentRange = new DateRange();
            ranges.Add(currentRange);
        }

        currentRange.Add(currentDate);
    }

    return ranges;
}

您还可以通过传递 IEnumerable< DateTime> :

public static IEnumerable<DateRange> GetRanges(this IEnumerable<DateTime> dates)
{
    List<DateRange> ranges = new List<DateRange>();
    DateRange currentRange = null;
    DateTime? previousDate = null;

    // this presumes a list of dates ordered by day, if not then the list will need sorting first
    foreach( var currentDate in dates )
    {
        if( previousDate == null || previousDate.Value != currentDate.AddDays(-1) )
        {
            // it's either the first date or the current date isn't consecutive to the previous so a new range is needed
            currentRange = new DateRange();
            ranges.Add(currentRange);
        }

        currentRange.Add(currentDate);
        previousDate = currentDate;
    }

    return ranges;
}

这篇关于从日期集合中查找日期范围C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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