bet*_*eta 1 c# iterator loops tuples list
In my C# code I have a list of Tuples. The tuples themselves consist of two numbers of the type double and an object of type LocalDate.
List<Tuple<double, double, LocalDate>> tupleList = new List<Tuple<double, double, LocalDate>>();
Run Code Online (Sandbox Code Playgroud)
The list, for instance, could look as follows.
1, 10, LocalDate1
12, 310, LocalDate2
31, 110, LocalDate3
Run Code Online (Sandbox Code Playgroud)
What is an elegant way to create an array of doubles that only contains the first double values of each list item?
Accordingly, I want an ArrayList that only consists of the LocalDate objects in the list. The order should be preserved.
The expected result should be:
double[] => [1, 12, 31]
double[] => [10, 310, 110]
ArrayList<> => [LocalDate1, LocalDate2, LocalDate3]
Run Code Online (Sandbox Code Playgroud)
I am aware that the ordinary way would be to iterate over the list in a for loop and create the arrays via this loop. However, I think that there should be a more concise and elegant way.
Linq would be the way to go:
var firstValues = tupleList.Select(x => x.Item1).ToList();
Run Code Online (Sandbox Code Playgroud)
这会将元组列表投影到仅第一个项目的列表中,并保持其顺序。与第二,第三,第n个项目相同。
如果你想要一个数组,只调用ToArray()代替ToList()。