How to use LINQ replace a range of nested values in a list?

1.3k Views Asked by At

I'm probably not wording the question correctly, so please help me on the nomenclature.

If I have a List<Point> which has a constant set of x values, but a variable stream of y values. Right now I have:

List<DataPoint> points = new List<DataPoint>();
while(...)
{
    double data[] = ...
    for (int i = 0; i < data.Length; i++)
        points.Add(new DataPoint(i, data[i]));
}

But I feel like it should be possible to use LINQ:

points.Select(y => y.AddRange(data));

I don't need to change the x values. Also, I'm asking because I'm trying to increase processing performance on this loop somehow, so if there's a faster way, please enlighten me.

1

There are 1 best solutions below

1
On

You can use Linq to create all the values you want to add then use AddRange.

var newPoints = data.Select((d,i) => new DataPoint(i, d));
points.AddRange(newPoints);

But please note that this will probably not be any faster than your for loop.