我需要在一天内获得最低的机票价格。 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(最小)

10-01 19:29