C# 如何对一个阵列使用linq的group by和min STANTATION?
我需要在一天内得到最低的航班价格<代码>飞行列表填写所有航班选项。在此数组中存在一天的航班数。我应该在一天内得到一个航班,这个航班应该有最低价格。我使用像这样的linq,但在本节中C# 如何对一个阵列使用linq的group by和min STANTATION?,c#,linq,C#,Linq,我需要在一天内得到最低的航班价格飞行列表填写所有航班选项。在此数组中存在一天的航班数。我应该在一天内得到一个航班,这个航班应该有最低价格。我使用像这样的linq,但在本节中Min(x=>x.PriceView)get error。如何解决这个问题 //AFlight is a class AFlight[] aFlightList = { new AFlight() }; aFlightList = charterService .GetFlightList(chfs.DepCityId
Min(x=>x.PriceView)
get error。如何解决这个问题
//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);
i分组
实现IEnumerable
。因此,您可以在i分组
中获取每个单独的项目,然后获取其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
排序要获取第一个(最小值)您可以使用GroupBy重载,该重载使用resultselector一次完成所有这些操作,而不是只传递1个lambda来选择键,而是传递第二个lambda来选择结果集:
var res = aFlightList
.GroupBy(item =>
// State that you want to group by date
item.FlightDate,
// Will be called for each pair, you don't use the key as it's already part of the original object you will return, here for each group you'll only return a single item, the lowest priced one and will end up with an IEnumerable containing the cheapest one of each group
(key, pairs) => pairs
.OrderBy(p => p.PriceView)
.First());
groupby的这个重载返回一个平面IEnumerable,因此您甚至不会得到带有集合中单个项的IGrouppings,而是带有平面对象的IGrouppings