我需要在一天内获得最低的航班价格。 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);
答案 0 :(得分:2)
你可以使用GroupBy的重载,它使一个resultselector一次完成所有这些操作,而不是只传递1个lambda来选择键,传递第二个来选择你的结果集:
function mostrarDatos ($resultado) {
if ($resultado !=NULL) {
echo "- Promedio de Categoria ".'Producto_Prioridad'." es:".$resultado['total_average']."<br/>";
}
else {
echo "<br/>No hay más datos!!! <br/>";
}
}
$link = mysqli_connect("db", "user", "pass");
mysqli_select_db($link, "db");
$promedio = mysqli_query($link, "SELECT AVG( Producto_Precio ) as total_average FROM`Natan_Productos` GROUP BY Producto_Prioridad");
while ($fila = mysqli_fetch_array($promedio)){ // loops 4 times and displays on screen. How to store into table as well
mostrarDatos($fila);
}
mysqli_free_result($promedio);
mysqli_close($link);
groupby的这个重载返回一个平坦的IEnumerable,所以你甚至不会在集合中使用单个项目但是使用扁平对象的IGroupings。
答案 1 :(得分:1)
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
(最低)