2012-03-22 51 views
2

我有以下的LINQ表达式从我的数据库中提取的所有数据:的LINQ的GroupBy过滤

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

现在我想组由现场ActualRoom只有用ActualRoom计数> 3

的那些是不是可能?

回答

6

您可以使用GroupBy,只要记住,你正在失去你已经这样做了排序我就开始这样做了排序前:

var groups = _meetingRepository.Select<SessionLocation>() 
           .GroupBy(x => x.ActualRoom) 
           .Where(g => g.Count() > 3) 

要有排序组 - 假设保留算作一个单独的属性时并不需要你可以直接投射到IEnumerable<SessionLocation>IEnumerable

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)); 
+0

谢谢你的快速回复。我完全理解失去排序,但可以在字典中排序结果吗? – user1286550 2012-03-22 18:16:12

+0

您希望每个组内的项目*排序? – BrokenGlass 2012-03-22 18:17:34

+0

是的。我希望每个组中的项目至少按SessionDT排序,并且如果SessionEndTime也可以排序。 – user1286550 2012-03-22 18:22:22