1

I have several enumerables with data from sensors

time_elapsed, speed_x, speed_y, speed_z, altitude, latitude, longitude…

Every list has the same number of elements.

I want to combine the data of all the lists into a sequence of Status items.

class Status 
{
    public int TimeElapsed {get; set; }
    public double SpeedX {get; set; }
    public double SpeedY {get; set; }
    public double SpeedZ {get; set; }
   ...
}

I thought about using the Enumerable.Zip method, but it looks really cumbersome:

var statuses = time_elapsed
    .Zip(speed_x, (a, b) => new { a,  b})
    .Zip(speed_y, (c, d) => new { c,  d})
    .Zip(speed_z, (e, f) => new { e , f})
    .Select(x => new Status
    {
        Time = x.e.c.a,
        SpeedX = x.e.c.b,
        SpeedY = x.e.d,
        SpeedZ = x.f
        // ...
    });

As you see, it's from from being readable with all those anonymous types.

Is there a better way to do it without losing your head?

itsme86
  • 19,266
  • 4
  • 41
  • 57
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 1
    https://stackoverflow.com/questions/44378137/optimizing-linq-combining-multiple-lists-into-new-generic-list/44378284 – Michael Liu Oct 08 '18 at 15:26

2 Answers2

3

Not much that you can do here, but you can improve readability and remove one anonymous type:

var statuses = time_elapsed
    .Zip(speed_x, (time, speedX) => new {time, speedX})
    .Zip(speed_y, (x, speedY) => new {x.time, x.speedX, speedY})
    .Zip(speed_z, (x, speedZ) => new Status
    {
        TimeElapsed = x.time,
        SpeedX = x.speedX,
        SpeedY = x.speedY,
        SpeedZ = speedZ
    });

You could also use this approach:

int minSize = new IList[] {time_elapsed, speed_x, speed_y, speed_z}.Min(c => c.Count);
IEnumerable<Status> statuses = Enumerable.Range(0, minSize)
    .Select(i => new Status
    {
        TimeElapsed = time_elapsed[i],
        SpeedX = speed_x[i],
        SpeedY = speed_y[i],
        SpeedZ = speed_z[i],
    });
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

The link from @Michael Liu pointed to some code from Jon Skeet that provides a clean solution to your problem which is just to create a custom zip that handles 3 sequences:

    static IEnumerable<TResult> Zip<TFirst, TSecond, TThird, TResult>(
    IEnumerable<TFirst> first,
    IEnumerable<TSecond> second,
    IEnumerable<TThird> third,
    Func<TFirst, TSecond, TThird, TResult> resultSelector)
    {
        using (IEnumerator<TFirst> iterator1 = first.GetEnumerator())
        using (IEnumerator<TSecond> iterator2 = second.GetEnumerator())
        using (IEnumerator<TThird> iterator3 = third.GetEnumerator())
        {
            while (iterator1.MoveNext() && iterator2.MoveNext() && iterator3.MoveNext())
            {
                yield return resultSelector(iterator1.Current, iterator2.Current, iterator3.Current);
            }
        }
    }
MikeJ
  • 1,299
  • 7
  • 10