获取c#中列表的列表中所有[x]索引的最大值

本文关键字:列表 索引 最大值 获取 | 更新日期: 2023-09-27 17:50:13

如果我在c#项目中有一个List<List<double>>对象,我怎么能得到该对象中每个List中所有[x]索引的最大值?

为了澄清我的想法,如果我有对象:

List<List<double>> myList = ......

,如果myList中每个列表中的[10]索引值为:

myList[0][10] = 5;
myList[1][10] = 15;
myList[2][10] = 1;
myList[3][10] = 3;
myList[4][10] = 7;
myList[5][10] = 5;

所以,我需要得到值15因为它是它们的最大值

谢谢,的问候。阿雅

获取c#中列表的列表中所有[x]索引的最大值

使用下列方法获取最大索引值

List<List<double>> list = ...
var maxIndex = list.Max( innerList => innerList.Count - 1); // Gets the Maximum index value.

如果想要最大值,可以使用

 var maxValue = list.Max ( innerList => innerList.Max());

参见Enumerable。马克斯


按注释编辑

我需要每个列表中特定索引的最大值。

未优化的解决方案是使用以下查询。

var index = 10;
var maxAtIndex10 = list.Max ( innerList => innerList[index]);

下面的查询是查找所有索引的最大值。

var maxIndex = list.Max( innerList => innerList.Count);
var listMaxAtAllIndexes = Enumerable.Range(0,maxIndex).Select ( index => list.Max(innerList => index < innerList.Count ? innerList[index] : 0));