1

約 1000 レコードの配列があり、各レコードは次のプロパティを持つオブジェクトです。

- 結果
    + 数値: 整数 (ID ではなく、繰り返すことができます)
    + 日付: DateTime

隣接する 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
4

2 に答える 2

0

簡潔にするために、順序付けられた入力を想定します

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 }
于 2011-07-15T10:20:34.803 に答える