Skip to content

Instantly share code, notes, and snippets.

View westonal's full-sized avatar

Alan Evans westonal

View GitHub Profile
@hickford
hickford / group-adjacent-by.cs
Created April 1, 2012 16:58
GroupAdjacentBy method for .NET in C#
public static class LINQExtensions
{
public static IEnumerable<IGrouping<TKey, TElement>> GroupAdjacentBy<TElement, TKey>(this IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer=null)
{
comparer = comparer ?? EqualityComparer<TKey>.Default;
List<TElement> elements = null;
TKey key = default(TKey);
TKey lastKey = default(TKey);
foreach (var x in source)
{