LINQ Split list into lists by date

I have list of objects List<Payment>. I would like to get list of lists grouped by PaymentDate. Something like that: List<List<Payments>> where each list of the list have same date (without hours).

Jon Skeet
people
quotationmark

You can either use GroupBy as suggested by others, or you can just create a Lookup:

var lookup = payments.ToLookup(payment => payment.PaymentDate.Date);

You can iterate over that (using each group's key as the date) or fetch all the payments for a given date using the indexer. I usually find that a lookup is cleaner than using a nested collection such as a List<List<...>> or a Dictionary<..., List<...>>.

people

See more on this question at Stackoverflow