如何处理多个Enumerable.Zip呼叫?

时间:2018-10-08 15:18:55

标签: c# .net linq enumerable

我有几个来自传感器的数据枚举

经过时间,速度x,速度y,速度z,高度,纬度,经度...

每个列表具有相同数量的元素。

我想将所有列表的数据合并为一系列状态项。

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

我考虑过使用Enumerable.Zip方法,但它看起来确实很麻烦:

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
        // ...
    });

如您所见,它来自所有匿名类型的可读性。

有没有更好的方法来做到这一点而又不失头呢?

2 个答案:

答案 0 :(得分:3)

您在这里不能做很多事情,但是可以提高可读性并删除一种匿名类型:

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
    });

您还可以使用这种方法:

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],
    });

答案 1 :(得分:1)

@Michael Liu的链接指向Jon Skeet的一些代码,该代码为您的问题提供了一种清晰的解决方案,即仅创建一个处理3个序列的自定义zip:

    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);
            }
        }
    }