且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何选择一个LINQ GROUPBY前N行

更新时间:2022-12-10 21:20:07

替换第一()取(2),并使用的SelectMany()

List<Booking> yetAnotherList = 
                 list.GroupBy(row => row.TourOperator)
                     .SelectMany(g => g.OrderBy(row => row.DepDate).Take(2)) 
                     .ToList();



更新:忘记了的SelectMany 第一次。要扁平化的结果(其中的SelectMany 一样),否则你得到的名单 IEnumerables

Update: Forgot the SelectMany the first time. You want to flatten the result (which SelectMany does), otherwise you get a list of IEnumerables.