Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq expression to find the max value of a List<List<int>>?

Tags:

c#

linq

Is there a cool Linq expression to find the max int value in a List<List<int>>?

Currently doing:

int maxValue = 0;
foreach(List<int> valueRow in values)
{
    // linq expression to get max value
    int value = valueRow.OfType<int>().Max(); 
    if (value > maxValue)
    {
        maxValue = value;
    }
}
like image 383
Seth Avatar asked Dec 04 '22 21:12

Seth


1 Answers

You can use SelectMany() for this which flattens the nested list, then you can just take the maximum of the resulting sequence:

int maxValue  = values.SelectMany( x => x).Max();
like image 80
BrokenGlass Avatar answered Dec 08 '22 12:12

BrokenGlass