using System; using System.Linq; class Foo { public int A { get; set; } public int B { get; set; } } public class Test { public static void Main() { var list = new System.Collections.Generic.List(){ new Foo(){ A = 1, B = 1 }, new Foo(){ A = 1, B = 2 }, new Foo(){ A = 2, B = 3 }, new Foo(){ A = 2, B = 4 }, new Foo(){ A = 1, B = 5 }, new Foo(){ A = 3, B = 6 } }; var groups = list .Select((f, i) => new { Obj = f, Next = list.ElementAtOrDefault(i + 1), Prev = list.ElementAtOrDefault(i - 1) }) .Select(x => new { A = x.Obj.A, x.Obj, Consecutive = (x.Next != null && x.Next.A == x.Obj.A) || (x.Prev != null && x.Prev.A == x.Obj.A) }) .GroupBy(x => new { x.Consecutive, x.A }); foreach (var abGroup in groups) { int aKey = abGroup.Key.A; var bList = string.Join(",", abGroup.Select(x => x.Obj.B.ToString()).ToArray()); Console.WriteLine("A = {0}, Bs = [ {1} ] ", aKey, bList); } } }