using System.Collections.Generic;
public Entry(string year, double service, double earnings)
this.Earnings = earnings;
public override string ToString()
return String.Format("{0} {1} {2}", this.Year, this.Service, this.Earnings);
public static void Main()
var list = new List<Entry>(){
new Entry("20002001", 1 , 100.00),
new Entry("20012002", .5 , 100.00),
new Entry("20022003", 1.0 , 100.00),
new Entry("20042005", 1.0 , 50.00),
new Entry("20052006", 1.0 , 50.00)};
var result = list.GroupAdjacentBy((x, y) => x.Service == 1 && y.Service == 1).
g.First().Year.Substring(0,4) + g.Last().Year.Substring(4,4),
g.Sum(e => e.Earnings)));
Console.WriteLine(result.ElementAt(2));
public static class LinqExtensions
public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(
this IEnumerable<T> source, Func<T, T, bool> predicate)
using (var e = source.GetEnumerator())
var list = new List<T> { e.Current };
if (predicate(pred, e.Current))
list = new List<T> { e.Current };