Is this line possible:
.OrderByDescending (m => m.Max (x => x.date ) )
sorts groups by their maximum date, not by items in each group?
This cut off code segment gives the results you are looking for (although you will have to work with file processing, obviously)
List<Customer> Customers = new List<Customer>() { new Customer(){ RecordId = 12, Birthday = new DateTime(1970, 1, 1)}, new Customer(){ RecordId = 12, Birthday = new DateTime(1982, 3, 22)}, new Customer(){ RecordId = 12, Birthday = new DateTime(1990, 1, 1)}, new Customer(){ RecordId = 14, Birthday = new DateTime(1960, 1, 1)}, new Customer(){ RecordId = 14, Birthday = new DateTime(1990, 5, 15)}, }; var groups = Customers.GroupBy(c => c.RecordId); IEnumerable<Customer> itemsFromGroupWithMaxDate = groups.Select(g => g.OrderByDescending(c => c.Birthday).First()); foreach(Customer C in itemsFromGroupWithMaxDate) Console.WriteLine(String.Format("{0} {1}", C.RecordId, C.Birthday));
Or even better:
IEnumerable<Customer> itemsFromGroupWithMaxDate = Customers.GroupBy(c => c.RecordId).Select(g => g.OrderByDescending(c => c.Birthday).First());
Blind hit on your code, I believe this might work:
var recipients = File.ReadAllLines(path) .Select (record => record.Split('|')) .Select (tokens => new { FirstName = tokens[2], LastName = tokens[4], recordId = Convert.ToInt32(tokens[13]), date = Convert.ToDateTime(tokens[17]) } ) .GroupBy (m => m.recordId) .Select(m => OrderByDescending(x => x.date).First()) .OrderBy (m => m.recordId ) .Dump();