gpt4 book ai didi

c# - 如何对一个数组使用 linq 的 group by 和 min 语句?

转载 作者:太空宇宙 更新时间:2023-11-03 23:16:15 26 4
gpt4 key购买 nike

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

最佳答案

您可以使用带有结果选择器的 GroupBy 重载一次完成所有这些操作,而不是只传递 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,因此您甚至不会以集合中的单个项目但具有平面对象的 IGroupings 结束。

关于c# - 如何对一个数组使用 linq 的 group by 和 min 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37181709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com