How do I select every 6th element from a list (using Linq)

Posted by iDog on Stack Overflow See other posts from Stack Overflow or by iDog
Published on 2010-03-16T11:10:05Z Indexed on 2010/03/16 11:16 UTC
Read the original article Hit count: 147

Filed under:
|

Hi, I've got a list of 'double' values. I need to select every 6th record. It's a list of coordinates, where I need to get the minimum and maximum value of every 6th value.

List of coordinates (sample): [2.1, 4.3, 1.0, 7.1, 10.6, 39.23, 0.5, ... ] with hundrets of coordinates.

Result should look like: [x_min, y_min, z_min, x_max, y_max, z_max] with exactly 6 coordinates.

Following code works, but it takes to long to iterate over all coordinates. I'd like to use Linq instead (maybe faster?)

for (int i = 0; i < 6; i++)
{
    List<double> coordinateRange = new List<double>();

    for (int j = i; j < allCoordinates.Count(); j = j + 6)
        coordinateRange.Add(allCoordinates[j]);

    if (i < 3) boundingBox.Add(coordinateRange.Min());
    else boundingBox.Add(coordinateRange.Max());
}

Any suggestions? Many thanks! Greets!

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ