如何在 Linq GroupBy 中选择前 N 行
希望你能在这里帮助我。 我有一个预订列表,我想在其中获取每组 TourOperators 中的前 2 行。
这是数据示例:
List<Booking> list = new List<Booking>();
list.Add(new Booking() { BookingNo = "31111111", DepDate = new DateTime(2011, 5, 1), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "32222222", DepDate = new DateTime(2011, 5, 2), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "33333333", DepDate = new DateTime(2011, 5, 3), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "34444444", DepDate = new DateTime(2011, 5, 4), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "35555555", DepDate = new DateTime(2011, 5, 5), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "36666666", DepDate = new DateTime(2011, 5, 6), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "11111111", DepDate = new DateTime(2011, 5, 1), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "12222222", DepDate = new DateTime(2011, 5, 2), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "13333333", DepDate = new DateTime(2011, 5, 3), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "14444444", DepDate = new DateTime(2011, 5, 4), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "15555555", DepDate = new DateTime(2011, 5, 5), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "16666666", DepDate = new DateTime(2011, 5, 6), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "17777777", DepDate = new DateTime(2011, 5, 7), TourOperator = "VIN" });
我有这个 Linq,但它只给我每组中的第一个预订:
List<Booking> yetAnotherList = list.GroupBy(row => row.TourOperator)
.Select(g => g.OrderBy(row => row.DepDate).First()).ToList();
我希望的数据是这样的:
31111111, 2011-05-01, SPI
32222222, 2011-05-02, SPI
11111111, 2011-05-01, 车辆识别号
12222222, 2011-05-02, VIN
谢谢。
hope you can help me out here.
I have a list of Bookings where I would like to get the top 2 rows in each group of TourOperators.
here's a sample of the data:
List<Booking> list = new List<Booking>();
list.Add(new Booking() { BookingNo = "31111111", DepDate = new DateTime(2011, 5, 1), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "32222222", DepDate = new DateTime(2011, 5, 2), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "33333333", DepDate = new DateTime(2011, 5, 3), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "34444444", DepDate = new DateTime(2011, 5, 4), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "35555555", DepDate = new DateTime(2011, 5, 5), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "36666666", DepDate = new DateTime(2011, 5, 6), TourOperator = "SPI" });
list.Add(new Booking() { BookingNo = "11111111", DepDate = new DateTime(2011, 5, 1), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "12222222", DepDate = new DateTime(2011, 5, 2), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "13333333", DepDate = new DateTime(2011, 5, 3), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "14444444", DepDate = new DateTime(2011, 5, 4), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "15555555", DepDate = new DateTime(2011, 5, 5), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "16666666", DepDate = new DateTime(2011, 5, 6), TourOperator = "VIN" });
list.Add(new Booking() { BookingNo = "17777777", DepDate = new DateTime(2011, 5, 7), TourOperator = "VIN" });
I have this Linq, but it only gives me the first Booking in each group:
List<Booking> yetAnotherList = list.GroupBy(row => row.TourOperator)
.Select(g => g.OrderBy(row => row.DepDate).First()).ToList();
The data I was hoping for was something like this:
31111111, 2011-05-01, SPI
32222222, 2011-05-02, SPI
11111111, 2011-05-01, VIN
12222222, 2011-05-02, VIN
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
First()
替换为Take(2)
并使用SelectMany()
:更新:忘记了
第一次选择Many。您想要展平结果(
SelectMany
就是这么做的),否则您会得到一个IEnumerables
列表。Replace
First()
withTake(2)
and useSelectMany()
:Update: Forgot the
SelectMany
the first time. You want to flatten the result (whichSelectMany
does), otherwise you get a list ofIEnumerables
.尝试使用
var
并将First()
替换为Take(2)
:Try to use
var
and replaceFirst()
withTake(2)
: