2

There's got to be a better way!

I have a bunch of log records stored in a List. Each log record has a CreateDate field and what I'm trying to do is find an efficient way to query the List object to find the day of the week with the most log records (Monday, Tuesday, etc...).

I'm thinking that this can be done using Linq but I don't know Linq well enough. It may be that I'll have to do this the long way by getting a count for each specific day and then comparing them against each other to find the day with the most logs but I'm hoping that someone will be able to show me a cool way to do it.

Thanks.

4

1 回答 1

1
Loglist.GroupBy(log => log.CreateDate)
    .Select(list => new { DayOfWeek = list.Key.DayOfWeek, Count = list.Count()})
    .OrderByDescending(list=>list.Count);

这将按降序返回 (DayOfWeek, Count) 列表。如果您只需要最大计数 dayofweek,.First()请在上述列表末尾申请。

于 2010-11-12T01:00:33.743 回答