Selecting from a list of lists in a lambda expression, I access the elements with
ElementAt
. However, it would be nicer to have the elements of my list directly as parameters for my lamda expression:
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main()
{
List<List<int>> mylist = new List<List<int>>();
mylist.Add(new List<int>{1,2});
mylist.Add(new List<int>{3,4});
// returns a list with summed values of the input lists
List <int> sums = mylist.Select(x => x.ElementAt(0) +x.ElementAt(1)).ToList();
// Can I somehow use the elements in my lists as parameters to lambda?
// List <int> sums = mylist.Select((x, y) => x + y).ToList();
}
}
This is possible for tuples C# 7 tuples and lambdas
But can I do this without converting my list to Tuples?