using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public class Cls
{
public int SequenceNumber { get; set; }
public int Value { get; set; }
}
public static void Main()
{
var classes = new List<Cls>() {
new Cls{SequenceNumber=1,Value=9},new Cls{SequenceNumber=2,Value=9}, new Cls{SequenceNumber=3,Value=15},
new Cls{SequenceNumber=4,Value=15},new Cls{SequenceNumber=5,Value=15}, new Cls{SequenceNumber=6,Value=30},
new Cls{SequenceNumber=7,Value=9}
};
var result = classes
.GroupAdjacent(c => c.Value)
.Select(g => new {
SequenceNumFrom = g.Min(c => c.SequenceNumber),
SequenceNumTo = g.Max(c => c.SequenceNumber),
Value = g.Key
});
foreach (var x in result)
Console.WriteLine("SequenceNumFrom:{0} SequenceNumTo:{1} Value:{2}", x.SequenceNumFrom, x.SequenceNumTo, x.Value);
}
}
public class GroupOfAdjacent<TSource, TKey> : IEnumerable<TSource>, IGrouping<TKey, TSource>
{
public TKey Key { get; set; }
private List<TSource> GroupList { get; set; }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((System.Collections.Generic.IEnumerable<TSource>)this).GetEnumerator();
}
System.Collections.Generic.IEnumerator<TSource> System.Collections.Generic.IEnumerable<TSource>.GetEnumerator()
{
foreach (var s in GroupList)
yield return s;
}
public GroupOfAdjacent(List<TSource> source, TKey key)
{
GroupList = source;
Key = key;
}
}
public static class Extensions
{
public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
TKey last = default(TKey);
bool haveLast = false;
List<TSource> list = new List<TSource>();
foreach (TSource s in source)
{
TKey k = keySelector(s);
if (haveLast)
{
if (!k.Equals(last))
{
yield return new GroupOfAdjacent<TSource, TKey>(list, last);
list = new List<TSource>();
list.Add(s);
last = k;
}
else
{
list.Add(s);
last = k;
}
}
else
{
list.Add(s);
last = k;
haveLast = true;
}
}
if (haveLast)
yield return new GroupOfAdjacent<TSource, TKey>(list, last);
}
}
dXNp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iBzOwogICAgICAgIH0KICAgICAgICBwdWJsaWMgR3JvdXBPZkFkamFjZW50KExpc3Q8VFNvdXJjZT4gc291cmNlLCBUS2V5IGtleSkKICAgICAgICB7CiAgICAgICAgICAgIEdyb3VwTGlzdCA9IHNvdXJjZTsKICAgICAgICAgICAgS2V5ID0ga2V5OwogICAgICAgIH0KICAgIH0KICAgIAogICAgcHVibGljIHN0YXRpYyBjbGFzcyBFeHRlbnNpb25zCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyBJRW51bWVyYWJsZTxJR3JvdXBpbmc8VEtleSwgVFNvdXJjZT4+IEdyb3VwQWRqYWNlbnQ8VFNvdXJjZSwgVEtleT4oCiAgICAgICAgICAgIHRoaXMgSUVudW1lcmFibGU8VFNvdXJjZT4gc291cmNlLAogICAgICAgICAgICBGdW5jPFRTb3VyY2UsIFRLZXk+IGtleVNlbGVjdG9yKQogICAgICAgIHsKICAgICAgICAgICAgVEtleSBsYXN0ID0gZGVmYXVsdChUS2V5KTsKICAgICAgICAgICAgYm9vbCBoYXZlTGFzdCA9IGZhbHNlOwogICAgICAgICAgICBMaXN0PFRTb3VyY2U+IGxpc3QgPSBuZXcgTGlzdDxUU291cmNlPigpOwogICAgICAgICAgICBmb3JlYWNoIChUU291cmNlIHMgaW4gc291cmNlKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBUS2V5IGsgPSBrZXlTZWxlY3RvcihzKTsKICAgICAgICAgICAgICAgIGlmIChoYXZlTGFzdCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZiAoIWsuRXF1YWxzKGxhc3QpKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgeWllbGQgcmV0dXJuIG5ldyBHcm91cE9mQWRqYWNlbnQ8VFNvdXJjZSwgVEtleT4obGlzdCwgbGFzdCk7CiAgICAgICAgICAgICAgICAgICAgICAgIGxpc3QgPSBuZXcgTGlzdDxUU291cmNlPigpOwogICAgICAgICAgICAgICAgICAgICAgICBsaXN0LkFkZChzKTsKICAgICAgICAgICAgICAgICAgICAgICAgbGFzdCA9IGs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGxpc3QuQWRkKHMpOwogICAgICAgICAgICAgICAgICAgICAgICBsYXN0ID0gazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbGlzdC5BZGQocyk7CiAgICAgICAgICAgICAgICAgICAgbGFzdCA9IGs7CiAgICAgICAgICAgICAgICAgICAgaGF2ZUxhc3QgPSB0cnVlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChoYXZlTGFzdCkKICAgICAgICAgICAgICAgIHlpZWxkIHJldHVybiBuZXcgR3JvdXBPZkFkamFjZW50PFRTb3VyY2UsIFRLZXk+KGxpc3QsIGxhc3QpOwogICAgICAgIH0KICAgIH0K