.net – Create List of Tuples from List using LINQ


I'm trying to create a list of tuples from a list using LINQ, but can't work out how to do it. What I've got is various data in an external file, which I'm reading sections using a standard method into a List<Single>, I then need to turn this into lists of groups of sequential elements of this list (which may have a variable number of elements in the groups). To state it another way:

List<Single> with n elements goes to List<Tuple<Single, Single>> with (n/2) elements


List<Single> with n elements goes to List<Tuple<Single, Single, Single>> with (n/3) elements

I couldn't work out how to do this with LINQ, so I've reverted to a for loop like, for example, so:

For i As Integer = 0 To CType(coords.Item2.Count / 3, Integer) Step 3
    normalList.Add( _
        New Tuple(Of Single, Single, Single)( _
            coords.Item2.Item(i), _
            coords.Item2.Item(i + 1), _
            coords.Item2.Item(i + 2) _
        ) _
Next i

EDIT: I'm not at all worried about a generic solution, I'm interested in if it's possible to do this type of thing using LINQ. It seems to be outside the score of what it's intended for, but I don't have enough of a feel to know if this is true.

My question is is is it possible to accomplish the above type of task using LINQ. I've bashed around in the UI and google but have come up empty!

Best Solution

Maybe this will help someone.

var pairs = from p in TheListOfAllLists select (
new Tuple<string, string>( p.ParameterName,p.Value.ToString()));