I have a requirement to sort the list by 1) the number of times a separate item appears, and then 2) if the number of two different lines matches the most recently used date of this group.
My function group and sorting by account work without date:
(from x in data group x by new { x.Col1, x.Col2, x.Col3} into g let count = g.Count() select new { g.Key.Col1, g.Key.Col2, g.Key.Col3, count }).OrderByDescending(x => x.count)
However, I was not able to successfully add the date sort. I tried to add a date column as a collection in a group by expression, but this does not work.
(from x in data group x by new { x.Col1, x.Col2, x.Col3, MaxDate = x.CreatedDateTime.Max()} into g let count = g.Count() select new { g.Key.Col1, g.Key.Col2, g.Key.Col3, count, g.Key.MaxDate }).OrderByDescending(x => x.count).ThenByDescending(x => x.MaxDate)
I understand why this does not work, I just canβt think of another route to add a secondary sort. Any ideas are welcome!
source share