我认为这是
var enumerable = Mix(dict.Keys, dict.Values);
我相信 .NET framework 4.0 Enumerable.Zip 会接近[1]
所以我找到了时间来实现这个MixSequences 方法,请注意我是如何将它做成 n 元的,所以它可以组合任意数量的序列(不仅仅是 2 个) .
using System;
using System.Linq;
using System.Collections.Generic;
namespace NS
{
static class Program
{
private static IEnumerable<T> MixSequences<T> (params IEnumerable<T>[] sequences)
{
var se = sequences.Select(s => s.GetEnumerator()).ToList();
try
{
while (se.All(e => e.MoveNext()))
foreach (var v in se.Select(e => e.Current))
yield return v;
}
finally
{ se.ForEach(e => e.Dispose()); }
}
public static void Main(string[] args)
{
var dict = new Dictionary<int,int>{ {1,4},{13,8},{2,1} };
var twin = new Dictionary<int,int>{ {71,74},{83,78},{72,71} };
Console.WriteLine("Keys: {0}", string.Join(", ", dict.Keys));
Console.WriteLine("Values: {0}", string.Join(", ", dict.Values));
Console.WriteLine("Proof of pudding: {0}", string.Join(", ", MixSequences(dict.Keys, dict.Values)));
Console.WriteLine("For extra super fun: {0}", string.Join(", ", MixSequences(dict.Keys, twin.Keys, dict.Values, twin.Values)));
}
}
}
干杯
[1] 更新背景见here、here或here。