计数数组中相邻的日期

本文关键字:日期 数数 数组 | 更新日期: 2023-09-27 18:02:16

我有一个大约1000条记录的数组,每条记录是一个具有以下属性的对象:

<>之前——结果+ Number:整数(可以重复,不能是ID)+ Date:日期时间之前

我想计数并获得与Date相邻的大部分Result。
有人能给点主意吗?

编辑:

例如:

I have an array:
List<Result> results = new List<Result> {
    new Result(){ Number = 34, Date = "7/15/2011" },
    new Result(){ Number = 59, Date = "7/15/2011" },
    new Result(){ Number = 11, Date = "7/15/2011" },
    new Result(){ Number = 1, Date = "7/16/2011" },
    new Result(){ Number = 3, Date = "7/16/2011" },
    new Result(){ Number = 34, Date = "7/16/2011" },
    new Result(){ Number = 98, Date = "7/17/2011" },
    new Result(){ Number = 34, Date = "7/17/2011" },
    new Result(){ Number = 29, Date = "7/17/2011" },
    new Result(){ Number = 99, Date = "7/17/2011" },
    new Result(){ Number = 1, Date = "7/17/2011" },
    new Result(){ Number = 31, Date = "7/18/2011" },
    new Result(){ Number = 76, Date = "7/18/2011" },
    new Result(){ Number = 47, Date = "7/18/2011" },
    new Result(){ Number = 34, Date = "7/18/2011" },
    new Result(){ Number = 99, Date = "7/19/2011" },
    new Result(){ Number = 0, Date = "7/19/2011" },
    new Result(){ Number = 88, Date = "7/19/2011" },
    new Result(){ Number = 33, Date = "7/19/2011" }
};
As you see, all i want is to get:
- Number = 34 with 4 adjacent days 
- Number = 1 with 2 adjacent days

计数数组中相邻的日期

var HighestAdjacentDate =
    Results.OrderByDescending(Result => 
        Results.Count(R => 
            Math.Abs((R.Date - Result.Date).Days) == 1)).FirstOrDefault();

为简洁起见,假设为有序输入

var sample = new [] {
    new { Number = 1, Date = DateTime.Now },
    new { Number = 2, Date = DateTime.Now.AddDays(5) },
    new { Number = 3, Date = DateTime.Now.AddDays(6) },
    new { Number = 4, Date = DateTime.Now.AddDays(9) }
};
var adjacent = sample
    .Where((o,i) => i>0 && (o.Date.Date - sample[i-1].Date.Date).Days<=1);
Console.WriteLine(string.Join("'n", adjacent));

对于这个特定的输入,显示

H:'>csharp test.cs
{ Number = 3, Date = 7/21/2011 12:19:44 PM }