Linq GroupBy过滤器

use*_*550 2 linq group-by

我有以下linq表达式从我的数据库中提取所有数据:

var items = response.Select(a => a.SessionLocationID).ToArray();
mdl = _meetingRepository.Select<SessionLocation>()
    .OrderBy(a => a.SessionDT).ThenBy(a => a.SessionEndTime);
Run Code Online (Sandbox Code Playgroud)

现在我想按ActualRoom字段分组,只有ActualRoom计数> 3的分组

那可能吗?

Bro*_*ass 6

您可以使用GroupBy,请记住您正在丢失已经执行的排序,因此我会在您进行排序之前开始:

var groups =  _meetingRepository.Select<SessionLocation>()
                                .GroupBy(x => x.ActualRoom)
                                .Where(g => g.Count() > 3)
Run Code Online (Sandbox Code Playgroud)

分类组-假设保持计数作为一个单独的属性时并不需要你可以直接投射到IEnumerableIEnumerable<SessionLocation>:

var groups =  _meetingRepository.Select<SessionLocation>()
                                .GroupBy(x => x.ActualRoom)
                                .Where(g => g.Count() > 3)
                                .Select(g => g.OrderBy(x => x.SessionDT).ThenBy(x => x.SessionEndTime));
Run Code Online (Sandbox Code Playgroud)