次のデータがあるとします。
時間 ステータス
10:00 オン
11:00 オフ
12:00 オフ
13:00 オフ
14:00 オフ
15:00 オン
16:00 オン
Linq を使用して次のようなものにグループ化するにはどうすればよいですか
[オン、[10:00]]、[オフ、[11:00、12:00、13:00、14:00]]、[オン、[15:00、16:00]]
ここGroupAdjacent
にリストされているような拡張機能を作成します。
そして、それは次のように簡単です:
var groups = myData.GroupAdjacent(data => data.OnOffStatus);
次のように、変数を使用して変更を追跡する 1 つの Linq クエリでこれを行うこともできます。
int key = 0;
var query = data.Select(
(n,i) => i == 0 ?
new { Value = n, Key = key } :
new
{
Value = n,
Key = n.OnOffFlag == data[i - 1].OnOffFlag ? key : ++key
})
.GroupBy(a => a.Key, a => a.Value);
基本的に、現在のアイテムが前のアイテムと等しくない場合に増加する各アイテムにキーを割り当てます。もちろん、これはデータがリストまたは配列にあることを前提としています。そうでない場合は、別の方法を試す必要があります
これは、連続した要素を比較して連続したキーを生成するために使用するハードコアLINQ ソリューションです。Enumerable.Zip
var adj = 0;
var t = data.Zip(data.Skip(1).Concat(new TimeStatus[] { null }),
(x, y) => new { x, key = (x == null || y == null || x.Status == y.Status) ? adj : adj++ }
).GroupBy(i => i.key, (k, g) => g.Select(e => e.x));
それはとして行うことができます。
TakeWhile<Predicate>
条件は、コレクションの最初の要素のオンまたはオフのテキストです。それが役に立てば幸い..
クラスを定義するなど、リストを解析して連続したキーを割り当てることができます。
public class TimeStatus
{
public int ContiguousKey { get; set; }
public string Time { get; set; }
public string Status { get; set; }
}
ループを繰り返し、カウントを維持し、ステータスがオンからオフに変化したことを検出するなどして、連続するキーに値を割り当てます。これにより、次のようなリストが得られます。
List<TimeStatus> timeStatuses = new List<TimeStatus>
{
new TimeStatus { ContiguousKey = 1, Status = "On", Time = "10:00"},
new TimeStatus { ContiguousKey = 1, Status = "On", Time = "11:00"},
new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "12:00"},
new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "13:00"},
new TimeStatus { ContiguousKey = 2, Status = "Off", Time = "14:00"},
new TimeStatus { ContiguousKey = 3, Status = "On", Time = "15:00"},
new TimeStatus { ContiguousKey = 3, Status = "On", Time = "16:00"}
};
次に、次のクエリを使用して、ステータスとグループ化された時間を抽出できます。
var query = timeStatuses.GroupBy(t => t.ContiguousKey)
.Select(g => new { Status = g.First().Status, Times = g });