我需要在一天内获得最低的机票价格。 aFlightList
填写航班的所有选项。在此阵列中,有一天的航班数。我应该在一天之内得到一次航班,该航班应该有最低价格。我使用这样的linq,但是在本节Min(x => x.PriceView)
中会出错。如何解决这个问题?
//AFlight is a class
AFlight[] aFlightList = { new AFlight() };
aFlightList = charterService
.GetFlightList(chfs.DepCityId, true, chfs.ArrCityId, true,
chfs.Fromdate.ToString("yyyy/MM/dd"),
chfs.Fromdate.AddDays(chfs.DateRange - 1).ToString("yyyy/MM/dd"),
authentication);
aFlightList = (AFlight[])aFlightList
.GroupBy(x => x.FlightDate)
.Min(x => x.PriceView);
最佳答案
IGrouping
实现IEnumerable
。因此,您可以在IGrouping
中获取每个项目,然后获取其PriceView
并查找最小值,如下所示:
AFlight[] aFlightList = { new AFlight() };
aFlightList = charterService.GetFlightList(chfs.DepCityId, true, chfs.ArrCityId, true, chfs.Fromdate.ToString("yyyy/MM/dd"), chfs.Fromdate.AddDays(chfs.DateRange - 1).ToString("yyyy/MM/dd"), authentication);
aFlightList = aFlightList
.GroupBy(x => x.FlightDate);
.Select(g => g.OrderBy(x => x.PriceView).First())
.ToArray();
关键是获取组的
IEnumerable
并按PriceView
排序以获得First
(最小)