karra karra - 2 months ago 9
C# Question

Mapping two lists of points and replace with linq

I have two lists that contains points (xy). The x-values are time and the y-values are sensor data. The two lists mostly share the same time stamp.

I would like to map the time stamps in both lists and substitute the x value in list A with the corresponding (time wise) y-value in list B.

The following image illustrates what I'm looking for:

enter image description here

Does anybody know how to do this with linq?

Answer

This looks like a straight forward join.

var desired = from a in ListA
              join b in ListB on a.Time equals b.Time
              select new 
              {
                  AValue = a.Value, 
                  BValue = b.Value
              };

Or in method syntax

var desired = ListA.Join(
    ListB, 
    a => a.Time, 
    b => b.Time, 
    (a,b) => new {AValue = a.Value, BValue = b.Value});
Comments