score:2
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);
}
Source: stackoverflow.com
Related Query
- get a List of Max values across a list of lists
- Linq get values not shared across multiple lists
- get values from nested Dictionary using LINQ. Values of Dictionary are list of lists
- Get a list of distinct max values
- Linq query to join 2 lists of objects and get the altered values as list
- How to get the index from 2 different lists which matches values from 3rd list
- Get a list of distinct values in List
- Get indexes of all matching values from list using Linq
- LINQ Get Distinct values and fill LIST
- How to get a list of the grouped values in linq groupby?
- Linq query to get the distinct values in a list
- Find the Second Max in a list of values using linq c#
- LINQ - get total count of values from nested list
- LINQ get Max value from list
- Summing values across nested lists at each index
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- Get Distinct List of Values from Nested Object
- Compare id's of objects in two lists and get the list of objects that includes objects with ids occurring in both of them
- Get list of distinct values in List<T> in c#
- How to get unique values from two list of dictionaries using Linq?
- Get the count of distinct elements from a list of lists using LINQ
- Get unique list of values from a dynamic list
- Find max possible Total from a list of player scores across their positions
- linq to get max of nullable datetime from a list
- Get distinct values from list
- Get maximum Count Lists Nested inside a list
- How to get sum of data in a list using multiple column values
- Get an enumerable of all sub lists of an existing list
- group a list and only get distinct values when input is a list
- Get values from Grouping result except values from list
More Query from same tag
- How to Distinct List<OrderDetails> based on one values from OrderDetails
- How to get aggregate data weekwise using Entity Framework
- Logging all Entity Framework queries in the debug window in DB-First Approach
- Compare 2 tables for changes in each columns
- System.Data.Linq.DuplicateKeyException on empty table
- Visual Studio 2015 missing XML comments / documentation
- Get index of lines that match my linq
- linq to group by country and then sum of dynamic generated columns
- Linq query between dates
- Accessing transparent identifier in select throws it is not referenced from scope
- Type system oddity: Enumerable.Cast<int>()
- Calculate average with linq without grouping
- Transforming List<string> into a tokenised string
- How to get distinct values separately in string array?
- Use Linq to filter on distinct options and newest date
- SQL Query or Linq query to get No of Events per day from database
- Get list of certain field back
- LINQ Performance for Large Collections
- C# LINQ -How to apply group by?
- Guidance with a translation in Linq to Xml
- How to get selected checkboxes in Grid using lambda expression
- Linq left join with group join
- Linq order by column, give priority to column with value
- Join multiple columns from the same table using Linq
- convert sql to linq with left outer join
- ef6 linq method returning $ref for nested entries in query
- Obtaining an intersection of two IEnumerables using LINQ
- How to do a twitter search using linq to twitter v.4 in c#
- Linq query to combine cell results?
- group by base on 2 element