score:2

Accepted answer

try this one:

 // here i declare your initial list.
 list<list<double>> list = new list<list<double>>()
 {
     new list<double>(){3,5,1},
     new list<double>(){5,1,8},
     new list<double>(){3,3,3},
     new list<double>(){2,0,4},
 };

 // that would be the list, which will hold the maxs.
 list<double> result = new list<double>();


 // find the maximum for the i-st element of all the lists in the list and add it 
 // to the result.
 for (int i = 0; i < list[0].count-1; i++)
 {
     result.add(list.select(x => x[i]).max());
 }

note: this solution works only, when all the lists that are contained in the list have the same number of elements.

score:-1

if always you know how many elements present in your lists,you can use this approach:

var result =  new[]
        {
            list.select(a => a[0]).max(), 
            list.select(a => a[1]).max(),
            list.select(a => a[2]).max()
        };

score:0

even if this topic is answered long time ago, i'd like to put here another solution i've made up with linq, shorter than this other solution :

list<list<int>> mylist; //initial list of list

list<list<int>> mins_list = mylist.aggregate(
    (x, cur) => cur.zip(x, (a, b) => (a.value > b.value) ? a : b).tolist()
).tolist();

this very simple code is just aggregating every sub-list into a list of minima. note that the internal tolist is mandatory as zip is deferred.

you can encapsulate the code in an extension method, and do the same trick as marcinjuraszek to generate other similar computations (min, max, mean, std, ...).

score:4

var source = new list<list<int>> {
    new list<int> { 3, 5, 1 },
    new list<int> { 5, 1, 8 },
    new list<int> { 3, 3, 3 },
    new list<int> { 2, 0, 4 }
};

var maxes = source.selectmany(x => x.select((v, i) => new { v, i }))
                  .groupby(x => x.i, x => x.v)
                  .orderby(g => g.key)
                  .select(g => g.max())
                  .tolist();

returns { 5, 5, 8}, which is what you need. and will work when source lists have different number of elements too.

bonus

if you need version for min too, and want to prevent code duplication, you can go a little bit functional:

private static ienumerable<tsource> getbyindex<tsource>(ienumerable<ienumerable<tsource>> source, func<ienumerable<tsource>, tsource> selector)
{
    return source.selectmany(x => x.select((v, i) => new { v, i }))
                 .groupby(x => x.i, x => x.v)
                 .orderby(g => g.key)
                 .select(g => selector(g));
}

public static ienumerable<tsource> getmaxbyindex<tsource>(ienumerable<ienumerable<tsource>> source)
{
    return getbyindex(source, enumerable.max);
}

public static ienumerable<tsource> getminbyindex<tsource>(ienumerable<ienumerable<tsource>> source)
{
    return getbyindex(source, enumerable.min);
}

Related Query

More Query from same tag