Reputation: 341
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
Upvotes: 0
Views: 1084
Reputation: 30698
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));
Upvotes: 8