0

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.

UndeadBob
  • 1,110
  • 1
  • 15
  • 34
  • 5
    LINQ is for querying, not modifying data. IMO, a loop would be much clear option when it comes to code readability. There might not be any performance gain with LINQ – Habib Feb 02 '16 at 19:14
  • You could probably convert data to a List and do a join with LINQ and assign the result to `points`. – Sonny Childs Feb 02 '16 at 19:20
  • *"...which has a **constant** set of x values"* sounds like array to me. – Ivan Stoev Feb 02 '16 at 19:27

1 Answers1

1

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.

juharr
  • 31,741
  • 4
  • 58
  • 93