要使用LINQ获取数组中最高值的索引,您可以使用以下代码:
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
int maxIndex = numbers.Select((value, index) => new { Value = value, Index = index })
.Aggregate((max, current) => current.Value > max.Value ? current : max).Index;
Console.WriteLine("最高值的索引是:" + maxIndex);
}
}
在这个示例中,我们首先创建了一个整数数组。然后,我们使用LINQ的Select方法将数组中的每个值与其索引一起存储在一个匿名类型中。接下来,我们使用LINQ的Aggregate方法遍历这些值和索引,并将最大值和其索引存储在一个匿名类型中。最后,我们从结果中提取最大值的索引并将其打印到控制台上。
云+社区沙龙online第5期[架构演进]
云+社区技术沙龙[第21期]
小程序·云开发官方直播课(数据库方向)
腾讯云存储专题直播
Elastic 中国开发者大会
Elastic 实战工作坊
Elastic 实战工作坊
云+社区技术沙龙[第17期]
领取专属 10元无门槛券
手把手带您无忧上云