Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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# 如果列表有多个连续月份,则返回true_C#_List_Datetime - Fatal编程技术网

C# 如果列表有多个连续月份,则返回true

C# 如果列表有多个连续月份,则返回true,c#,list,datetime,C#,List,Datetime,我正在处理一个C#list类型的DateTime,它有几行dd/mm/yyyyy格式的数据,我正在试图找到一个方法,如果以下三个条件之一为true 如果所有列表项都相等,即相等的月份和年份,这很简单 如果月份和年份是按顺序排列的 示例: 10/4/2016 10/3/2016 10/2/2016 10/1/2016 10/12/2015 在上面的示例中,月份和年份是按顺序排列的,因此该方法返回true 三,。如果列表中有多个连续月份 示例: 10/2/2016 10

我正在处理一个
C#list
类型的
DateTime
,它有几行
dd/mm/yyyyy
格式的数据,我正在试图找到一个方法,如果以下三个条件之一为
true

  • 如果所有列表项都相等,即相等的月份和年份,这很简单
  • 如果月份和年份是按顺序排列的
  • 示例:

    10/4/2016  
    10/3/2016   
    10/2/2016   
    10/1/2016   
    10/12/2015
    
    在上面的示例中,
    月份和年份
    是按顺序排列的,因此该方法返回
    true

    三,。如果列表中有多个连续月份

    示例:

    10/2/2016   
    10/1/2016   
    10/12/2015
    10/2/2016   
    10/1/2016   
    10/12/2015
    10/2/2016   
    10/1/2016   
    10/12/2015
    
    在上面的列表中,它有三个连续月份
    12/2015、1/2016、2/2016
    。因此,对于上面的列表,它应该返回
    true
    。它应该返回
    false
    ,即使一个月不按顺序

    我能够为第二种情况编写一个方法,在这种情况下,dd/yyyy应该使用以下方法按顺序排列

    for (var x = 1; x < terms.Count; ++x)
        {
            var d1 = terms[x - 1];
            var d2 = terms[x];
    
    
            if (d2.Year == d1.Year)
            {
                if ((d1.Month - d2.Month) != 1) return false;
                continue;
            }                              
    
            if ((d1.Year - d2.Year) != 1) return false;
            if (d1.Month != 1 || d2.Month != 12) return false;
        }
        return true;
    
    for(变量x=1;x

    <强>是否有类似的方法来检查第三条件?< /强>

    < P>我不使用月份和年份,而是考虑添加时间。

    DateTime currentDateTime;
    foreach(var d in terms)
    {
      if(currentDateTime == default(DateTime))
      {
        currentDateTime = d;
      }
      else
      {
        currentDateTime = currentDateTime.AddMonths(1);
        if(currentDateTime != d) return false;
      }
    }
    return true;
    
    对于相同序列的重复(这次为伪码):


    DateTime没有任何格式。我不确定你指的是什么to@Dev他指出,
    DateTime
    类型没有“格式”的概念。格式是日期时间的表示方式。DateTime不显示数据,它只包含特定日期和时间的所有组成部分。您的第三个条件不清楚。是否要检查相同的重复序列?或者你只是在寻找任意数量的序列,任意大小的序列,只要这些序列是有序的?你真的不清楚你在寻找什么。任何序列都可以被视为“多个条带”,其中每个条带的长度为1…你的意思是将其与列表中的第一项的当前日期时间ot进行比较吗?我对在第一个代码块中使用currentDateTime有点困惑它只是意味着作为循环变量。。。当前正在使用的值。如果对您更有意义,可以将其命名为currentValue吗?与default(DateTime)的比较本质上是对其进行初始化,但使用foreach循环时,基本上希望在查看第一个值时对其进行初始化。
    // Determine initial sequence
    while(currentDateTime.AddMonths(1) == nextDateTime) count++;
    
    // Make sure that the whole sequence is a multiple of the short sequence
    if(totalLength % count != 0) return false;
    
    // Check remaining sequences
    for(iteration in 1 .. totalLength / count)
      for(index in 1 .. count)
        if(terms[index] != terms[count * iteration + index]) return false;
    
    // No elements differed
    return true;