Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查daylist是否连续有几天_C#_List_Sorting - Fatal编程技术网

C# 检查daylist是否连续有几天

C# 检查daylist是否连续有几天,c#,list,sorting,C#,List,Sorting,我在用c语言编程。 我有一个日期时间列表,可能是这样的我只对几天感兴趣: 12/07/2013, 12/07/2013, 12/06/2013, 12/05/2013, 12/04/2013, 12/04/2013, 11/11/2013, 11/10/2013, 11/04/2013. 换句话说,daylist可以包含更多相同的日期。天数代表测量值,这就是为什么它可以包含更多同一天的数据。 我需要检查daylist是否连续3天: 我的代码如下所示: 以下是一个扩展方法,它将返回集合中任意连续

我在用c语言编程。 我有一个日期时间列表,可能是这样的我只对几天感兴趣:

12/07/2013,
12/07/2013,
12/06/2013,
12/05/2013,
12/04/2013,
12/04/2013,
11/11/2013,
11/10/2013,
11/04/2013.
换句话说,daylist可以包含更多相同的日期。天数代表测量值,这就是为什么它可以包含更多同一天的数据。 我需要检查daylist是否连续3天:

我的代码如下所示:


以下是一个扩展方法,它将返回集合中任意连续天数:

static class EnumerableExtensions
{
    public static IEnumerable<IEnumerable<DateTime>> GetConsecutiveDays(this IEnumerable<DateTime> data,
                                                                        int consecutiveDayCount)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }

        if (consecutiveDayCount < 2)
        {
            throw new ArgumentException("consecutiveDayCount should be greater than 1");
        }

        var days = data.Select(item => item.Date).Distinct().OrderBy(item => item);

        return days.Select((day, index) => days.Skip(index).Take(consecutiveDayCount).ToList())
                   .Where(group => group.First().AddDays(consecutiveDayCount - 1) == group.Last());
    }
}
试验方法使用NUnit和FluentAssertions:

[TestFixture]
public class ConsecutiveDaysTests
{
    [Test]
    public void ConsecutiveDayTest()
    {
        var dayList = new List<DateTime>
        {
            new DateTime(2013,12,07),
            new DateTime(2013,12,07),
            new DateTime(2013,12,06),
            new DateTime(2013,12,05),
            new DateTime(2013,12,04),
            new DateTime(2013,12,04),
            new DateTime(2013,11,11),
            new DateTime(2013,11,10),
            new DateTime(2013,11,04)
        };


        var result = dayList.GetConsecutiveDays(3).ToList();

        result.Should().HaveCount(2);
        result.First().ShouldBeEquivalentTo(new[]{new DateTime(2013,12,06),
                                                  new DateTime(2013,12,05),
                                                  new DateTime(2013,12,04)});
    }
}

连续三天意味着什么?连续三天还是连续三天相同?你是说连续三天?或是其中的3个?你们应该把你们的样本编码为1。制作一份新的清单2。对于从第二个元素到最后一个元素的循环。为每个元素添加一个从当前元素到上一个元素的新时间跨度。3.循环遍历时间跨度,并将一天的时间跨度加起来,如果不是,则将计数器重置为0。跟踪最大连续天数。样本数据不包含2013年9月11日。@Derek-Hmm如果我说您给我的过程只在列表连续2天而不是3天时返回我,我说的对吗?我似乎不太合适。我已经像这样做了公共静态void LongtimeAlysis1String cpr{var result=EnumerableExtensions.GetConcertiveDaysGetDaylist1CPR,3.ToList;},并使用字符串cpr输入从构造函数中调用了它。如果dayList有3个或更多连续的天,我需要该方法返回true或false。我对sry的编程非常陌生。@n.Stenvang使用这个:getDaylist1cpr.getConcertiveDays3.Any;
static class EnumerableExtensions
{
    public static IEnumerable<IEnumerable<DateTime>> GetConsecutiveDays(this IEnumerable<DateTime> data,
                                                                        int consecutiveDayCount)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }

        if (consecutiveDayCount < 2)
        {
            throw new ArgumentException("consecutiveDayCount should be greater than 1");
        }

        var days = data.Select(item => item.Date).Distinct().OrderBy(item => item);

        return days.Select((day, index) => days.Skip(index).Take(consecutiveDayCount).ToList())
                   .Where(group => group.First().AddDays(consecutiveDayCount - 1) == group.Last());
    }
}
[TestFixture]
public class ConsecutiveDaysTests
{
    [Test]
    public void ConsecutiveDayTest()
    {
        var dayList = new List<DateTime>
        {
            new DateTime(2013,12,07),
            new DateTime(2013,12,07),
            new DateTime(2013,12,06),
            new DateTime(2013,12,05),
            new DateTime(2013,12,04),
            new DateTime(2013,12,04),
            new DateTime(2013,11,11),
            new DateTime(2013,11,10),
            new DateTime(2013,11,04)
        };


        var result = dayList.GetConsecutiveDays(3).ToList();

        result.Should().HaveCount(2);
        result.First().ShouldBeEquivalentTo(new[]{new DateTime(2013,12,06),
                                                  new DateTime(2013,12,05),
                                                  new DateTime(2013,12,04)});
    }
}