Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对一个阵列使用linq的group by和min STANTATION?_C#_Linq - Fatal编程技术网

C# 如何对一个阵列使用linq的group by和min STANTATION?

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

我需要在一天内得到最低的航班价格<代码>飞行列表填写所有航班选项。在此数组中存在一天的航班数。我应该在一天内得到一个航班,这个航班应该有最低价格。我使用像这样的linq,但在本节中
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