This does what you're looking for and is generic:
private static IEnumerable<IGrouping<int, T>> GroupConsecutive<T>(this IEnumerable<T> set, Func<T, T, bool> predicate)
{
var i = 0;
var k = 0;
var ranges = from e in set
let idx = ++i
let next = set.ElementAtOrDefault(idx)
let key = (predicate(e, next)) ? k : k++
group e by key into g
select g;
return ranges;
}
Usage:
var set = new List<bool>
{
true,
false,
false,
false,
true,
true,
false,
false,
};
var groups = set.GroupConsecutive((b1, b2) => (b1 == b2));
foreach (var g in groups)
{
Console.WriteLine(g.Key);
foreach (var b in g)
Console.WriteLine("{0}", b);
}
Output:
0
True
1
False
False
False
2
True
True
3
False
False
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…