简体   繁体   中英

Getting the Max value in all [x] indexes in a list of lists in C#

If I have a List<List<double>> object in the C# project, how could I get the largest value of all [x] indexes in each List in that object?

To clarify my idea, if I have the object:

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

and if the values of the [10] index in each list in myList are:

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

So, I need to get the value 15 since it is the max of them.

Thanks, Regards. Aya

Use following for maximum index value

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

If you want maximum value, you can use

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

See also Enumerable.Max


EDIT as per comments

I need the max value of those in a specific index in each list.

An unoptimized solution is to use following query.

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

Following query is to find max at all indexes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM