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).
DateTime
has a Date
property:
List<List<Payments>> result = payments
.GroupBy(p => p.PaymentDate.Date)
.Select(g => g.ToList())
.ToList();
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<...>>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With