是否有一种灵活的方法可以使用LINQ将多个列表合并到一个列表中以有效地复制它?
public class RGB
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
public RGB(int red, int green, int blue) { Red = red; Green = green; Blue = blue; }
}
public void myFunction()
{
List<int> red = new List<int> { 0x00, 0x03, 0x06, 0x08, 0x09 };
List<int> green = new List<int> { 0x00, 0x05, 0x06, 0x07, 0x0a };
List<int> blue = new List<int> { 0x00, 0x02, 0x03, 0x05, 0x09 };
List<RGB> colors = new List<RGB>();
colors.Add(new RGB(red[0], green[0], blue[0]));
colors.Add(new RGB(red[1], green[1], blue[1]));
colors.Add(new RGB(red[2], green[2], blue[2]));
colors.Add(new RGB(red[3], green[3], blue[3]));
colors.Add(new RGB(red[4], green[4], blue[4]));
}
或者,由于列表单独到达,因此按顺序合并它们会更有效。
public class RGB
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
public RGB(int red, int green, int blue) { Red = red; Green = green; Blue = blue; }
}
public void myFunction()
{
List<int> red = new List<int> { 0x00, 0x03, 0x06, 0x08, 0x09 };
List<RGB> colors = new List<RGB>();
colors.Add(new RGB(red[0], 0, 0));
colors.Add(new RGB(red[1], 0, 0));
colors.Add(new RGB(red[2], 0, 0));
colors.Add(new RGB(red[3], 0, 0));
colors.Add(new RGB(red[4], 0, 0));
List<int> green = new List<int> { 0x00, 0x05, 0x06, 0x07, 0x0a };
colors[0].Green = green[0];
colors[1].Green = green[1];
colors[2].Green = green[2];
colors[3].Green = green[3];
colors[4].Green = green[4];
List<int> blue = new List<int> { 0x00, 0x02, 0x03, 0x05, 0x09 };
colors[0].Blue = blue[0];
colors[1].Blue = blue[1];
colors[2].Blue = blue[2];
colors[3].Blue = blue[3];
colors[4].Blue = blue[4];
}
答案 0 :(得分:24)
你基本上试图压缩三个集合。如果只支持LINQ Zip()
方法同时支持两个以上。但唉,它一次只支持两个。但我们可以让它发挥作用:
var reds = new List<int> { 0x00, 0x03, 0x06, 0x08, 0x09 };
var greens = new List<int> { 0x00, 0x05, 0x06, 0x07, 0x0a };
var blues = new List<int> { 0x00, 0x02, 0x03, 0x05, 0x09 };
var colors =
reds.Zip(greens.Zip(blues, Tuple.Create),
(red, tuple) => new RGB(red, tuple.Item1, tuple.Item2)
)
.ToList();
当然,编写扩展方法来做三个(或更多)并不是非常痛苦。
public static IEnumerable<TResult> Zip<TFirst, TSecond, TThird, TResult>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
IEnumerable<TThird> third,
Func<TFirst, TSecond, TThird, TResult> resultSelector)
{
using (var enum1 = first.GetEnumerator())
using (var enum2 = second.GetEnumerator())
using (var enum3 = third.GetEnumerator())
{
while (enum1.MoveNext() && enum2.MoveNext() && enum3.MoveNext())
{
yield return resultSelector(
enum1.Current,
enum2.Current,
enum3.Current);
}
}
}
这使事情变得更好:
var colors =
reds.Zip(greens, blues,
(red, green, blue) => new RGB(red, green, blue)
)
.ToList();
答案 1 :(得分:11)
是的 - 你可以这样做:
List<int> red = new List<int> { 0x00, 0x03, 0x06, 0x08, 0x09 };
List<int> green = new List<int> { 0x00, 0x05, 0x06, 0x07, 0x0a };
List<int> blue = new List<int> { 0x00, 0x02, 0x03, 0x05, 0x09 };
List<RGB> colors = Enumerable
.Range(0, red.Count)
.Select(i => new RGB(red[i], green[i], blue[i]))
.ToList();
答案 2 :(得分:3)
var colours = red.Select((t, i) => new RGB(t, green[i], blue[i])).ToList();
答案 3 :(得分:2)
这是一个简化版本,它采用相同类型的任意数量的序列(作为数组)并将它们拉到一起:
public static IEnumerable<TResult> Zip<T, TResult>(this IEnumerable<T>[] sequences, Func<T[], TResult> resultSelector)
{
var enumerators = sequences.Select(s => s.GetEnumerator()).ToArray();
while(enumerators.All(e => e.MoveNext()))
yield return resultSelector(enumerators.Select(e => e.Current).ToArray());
}
.Zip()
方法的另一个重载.Zip
每次添加一个序列答案 4 :(得分:1)
您可以使用Aggregate和Zip一次性压缩任意数量的IEnumebles。
以下是您如何使用您的示例执行此操作:
var colorLists = new List<int>[] { red, green, blue };
var rgbCount = red.Count;
var emptyTriples =
Enumerable.Repeat<Func<List<int>>>(() => new List<int>(), rgbCount)
.Select(makeList => makeList());
var rgbTriples = colorLists.Aggregate(
emptyTriples,
(partialTriples, channelValues) =>
partialTriples.Zip(
channelValues,
(partialTriple, channelValue) =>
{
partialTriple.Add(channelValue);
return partialTriple;
}));
var rgbObjects = rgbTriples.Select(
triple => new RGB(triple[0], triple[1], triple[2]));
通常,依赖Zip作为底层组合器避免了输入长度变化的问题。
答案 5 :(得分:0)
对于它的价值,我喜欢LINQ并经常使用它,但有时老式的方式是最好的。请注意以下示例:
const int Max = 100000;
var rnd = new Random();
var list1 = Enumerable.Range(1, Max).Select(r => rnd.Next(Max)).ToList();
var list2 = Enumerable.Range(1, Max).Select(r => rnd.Next(Max)).ToList();
DateTime start;
start = DateTime.Now;
var r1 = list1.Zip(list2, (a, b) => new { a, b }).ToList();
var time1 = DateTime.Now - start;
start = DateTime.Now;
var r2 = list1.Select((l1, i) => new { a = l1, b = list2[i]}).ToList();
var time2 = DateTime.Now - start;
start = DateTime.Now;
var r3 = new int[0].Select(i => new { a = 0, b = 0 }).ToList();
// Easy out-of-bounds prevention not offered in solution #2 (if list2 has fewer items)
int max = Math.Max(list1.Count, list2.Count);
for (int i = 0; i < max; i++)
r3.Add(new { a = list1[i], b = list2[i] });
var time3 = DateTime.Now - start;
Debug.WriteLine("r1 == r2: {0}", r1.SequenceEqual(r2));
Debug.WriteLine("r1 == r3: {0}", r1.SequenceEqual(r3));
Debug.WriteLine("time1 {0}", time1);
Debug.WriteLine("time2 {0}", time2);
Debug.WriteLine("time3 {0}", time3);
输出结果为:
r1 == r2:真实
r1 == r3:真实
时间01 00:00:00.0100071
时间2 00:00:00.0170138
时间00 00:00:00.0040028
当然,在这种情况下(人类的感知)时间几乎不可察觉所以归结为偏好,但知道#3是迄今为止最快的,我倾向于在类型的关键性能领域使用它更复杂或者可以使用的数据可能很大。
另外,请注意使用3时的区别:
const int Max = 100000;
var rnd = new Random();
var list1 = Enumerable.Range(1, Max).Select(r => rnd.Next(Max)).ToList();
var list2 = Enumerable.Range(1, Max).Select(r => rnd.Next(Max)).ToList();
var list3 = Enumerable.Range(1, Max).Select(r => rnd.Next(Max)).ToList();
DateTime start;
start = DateTime.Now;
var r1 = list1.Zip(list2, (a, b) => new { a, b }).Zip(list3, (ab, c) => new { ab.a, ab.b, c }).ToList();
var time1 = DateTime.Now - start;
start = DateTime.Now;
var r2 = list1.Select((l1, i) => new { a = l1, b = list2[i], c = list3[i] }).ToList();
var time2 = DateTime.Now - start;
start = DateTime.Now;
var r3 = new int[0].Select(i => new { a = 0, b = 0, c = 0 }).ToList();
// Easy out-of-bounds prevention not offered in solution #2 (if list2 or list3 have fewer items)
int max = new int[] { list1.Count, list2.Count, list3.Count }.Max();
for (int i = 0; i < max; i++)
r3.Add(new { a = list1[i], b = list2[i], c = list3[i] });
var time3 = DateTime.Now - start;
Debug.WriteLine("r1 == r2: {0}", r1.SequenceEqual(r2));
Debug.WriteLine("r1 == r3: {0}", r1.SequenceEqual(r3));
Debug.WriteLine("time1 {0}", time1);
Debug.WriteLine("time2 {0}", time2);
Debug.WriteLine("time3 {0}", time3);
输出:
r1 == r2:真实
r1 == r3:真实
time1 00:00:00.0280393
时间2 00:00:00.0089870
时间00 00:00:00.0050041
正如所料,.zip方法必须进行多次迭代,并且变得最慢。
答案 6 :(得分:0)
Jeff Mercado提供了一个解压缩三个序列的答案。这可以推广到任意数量的序列,但限制所有序列必须具有相同的项目类型。
这是一个通用的zip操作符,它可以处理不同的输入长度,并具有适当的错误处理和适当处理的枚举器:
static class EnumerableExtensions {
public static IEnumerable Zip<TSource, TResult>(
this IEnumerable<IEnumerable<TSource>> source,
Func<IEnumerable<TSource>, TResult> resultSelector
) {
if (source == null)
throw new ArgumentNullException("source");
if (resultSelector == null)
throw new ArgumentNullException("resultSelector");
var enumerators = new List<IEnumerator<TSource>>();
try {
foreach (var enumerable in source) {
if (enumerable == null)
throw new ArgumentNullException();
enumerators.Add(enumerable.GetEnumerator());
}
while (enumerators.Aggregate(true, (moveNext, enumerator) => moveNext && enumerator.MoveNext()))
yield return resultSelector(enumerators.Select(enumerator => enumerator.Current));
}
finally {
foreach (var enumerator in enumerators)
enumerator.Dispose();
}
}
}
然后可以使用此通用zip运算符计算颜色:
var reds = new[] { 0x00, 0x03, 0x06, 0x08, 0x09 };
var greens = new[] { 0x00, 0x05, 0x06, 0x07, 0x0a };
var blues = new[] { 0x00, 0x02, 0x03, 0x05, 0x09 };
var colors = new[] { reds, greens, blues }
.Zip(rgb => new RGB(rgb.First(), rgb.Skip(1).First(), rgb.Skip(2).First()));
代码可能不像其他一些解决方案那样优雅,但是在某些情况下,通用的zip运算符可能很有用,而且我提供的代码很有效,因为它只迭代每个源序列一次。
答案 7 :(得分:0)
像这样使用SelectMany:
List_A.Select(a => a.List_B).SelectMany(s => s).ToList();