首页 > 解决方案 > 获取响应规则的日期列表

问题描述

我有一个日期列表,例如:

  • 21/12/2019
  • 22/12/2019
  • 22/12/2019
  • 2019 年 12 月 24 日
  • 2019 年 12 月 26 日
  • 2019 年 12 月 26 日
  • 27/12/2019

我将规则模型定义为:

public class Timing
{
    public int PeriodNbMinutes { get; set; }
    public int NbTimes { get; set; }
    public bool IsConsecutive { get; set; }
    public int Multiplicity { get; set; }
}

现在我有这样的规则:我想找到所有日期:

在 2 天内出现 3 次和出现两次的日期

所以我应该得到两组(我不想重复使用组中已经存在的日期):

第一的

第一天:21/12/2019 - 22/12/2019 - 22/12/2019

第二

第二次:26/12/2019 - 26/12/2019 - 27/12/2019

然后我有这样的规则:

this.PeriodMinutes = 2680;
this.NbTimes = 2;
this.Multiplicity = 2;
this.IsConsecutive=true;

已编辑

    public int PeriodNbMinutes { get; set; }
    public int NbTimes { get; set; }
    public bool IsConsecutive { get; set; }
    public int Multiplicity { get; set; }

    internal bool Test(List<DateTime> metrics)
    {
        bool res = false;
        int times = 0;
        int multiple=0;
        TimeSpan ts = new TimeSpan(0, PeriodNbMinutes, 0);

        for (int i = 0; i < metrics.Count; i++)
        {
            var dates = metrics.Skip(i);

            var selectedDts = metrics.Zip(dates, (dtFirst, dtNext) => (dtNext - dtFirst).Ticks < ts.Ticks);

            if (selectedDts.Count() > 0)
            {
                res = true;
                times++;

                if (times == Multiplicity)
                {
                    multiple++;
                    if (multiple == Multiplicity)
                        return true;

                    // Consecutive???
                }
                else res = false;
            }
            else
            {
                res = false;
                times = 0;
            }
        }
        return res;

    }

如何使用 Linq 找到这些组?我尝试使用 TimeSpan 但无法正常工作。

此外,在所选的两组中,我需要知道它们是否连续。

我尝试了很多不同的解决方案,但没有什么好处。我怎样才能简单地做到这一点?

谢谢,

标签: c#linqdatetimecollections

解决方案


推荐阅读