using System; using System.Globalization; using System.Linq; using System.Collections.Generic; public class Test { public static void Main() { var words = new List{"golf", "hip", "hop", "hotel", "grass", "world", "wee"}; IEnumerable lastWordOfConsecutiveFirstCharGroups = words .GroupAdjacent(str => str[0]) .Select(g => g.Last()); Console.WriteLine(string.Join(",",lastWordOfConsecutiveFirstCharGroups.ToArray())); words=new List{"apples", "armies", "black", "beer", "bastion", "cat", "cart", "able", "art", "bark"}; lastWordOfConsecutiveFirstCharGroups = words .GroupAdjacent(str => str[0]) .Select(g => g.Last()); Console.WriteLine(string.Join(",",lastWordOfConsecutiveFirstCharGroups.ToArray())); } } public static class Extensions { public static IEnumerable> GroupAdjacent( this IEnumerable source, Func keySelector) { TKey last = default(TKey); bool haveLast = false; List list = new List(); foreach (TSource s in source) { TKey k = keySelector(s); if (haveLast) { if (!k.Equals(last)) { yield return new GroupOfAdjacent(list, last); list = new List(); 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(list, last); } public class GroupOfAdjacent : IEnumerable, IGrouping { public TKey Key { get; set; } private List GroupList { get; set; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return ((System.Collections.Generic.IEnumerable)this).GetEnumerator(); } System.Collections.Generic.IEnumerator System.Collections.Generic.IEnumerable.GetEnumerator() { foreach (var s in GroupList) yield return s; } public GroupOfAdjacent(List source, TKey key) { GroupList = source; Key = key; } } }