Merging few lists into one using linq
Is there a simple way to take few lists of the same size and create a new
one ouf of their min/max values in each index with linq? I mean:
List<List<int>> Some2dList = new List<List<int>>
{ new List<int> { 1, 2, 3, 4, 5 },
new List<int> { 5, 4, 3, 2, 1 } } ;
List<int> NewList = new List<int>(5);
for(int i=0; i< 5; i++)
{
int CurrentHighest=0;
for(int j=0; j<2; j++)
{
if (Some2dList[j][i] > CurrentHighest)
CurrentHighest = Some2dList[j][i];
}
NewList.Add(CurrentHighest);
}
//Which gives me {5,4,3,4,5}
I've simplified those loops to look clearlier. I know I could use Concat
and GroupBy, and then select Max out of each, but for simple types and
classes without a key value I can't figure it out.
No comments:
Post a Comment