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

Aya*_*hby 0 .net c# linq list max

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

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

List<List<double>> myList = ......
Run Code Online (Sandbox Code Playgroud)

如果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;
Run Code Online (Sandbox Code Playgroud)

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

感谢和问候.绫

Til*_*lak 8

使用以下内容获取最大索引值

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

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

 var maxValue = list.Max ( innerList => innerList.Max());
Run Code Online (Sandbox Code Playgroud)

另请参见Enumerable.Max


根据评论编辑

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

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

var index = 10;
var maxAtIndex10 = list.Max ( innerList => innerList[index]);
Run Code Online (Sandbox Code Playgroud)

以下查询是在所有索引中查找最大值.

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