合并多个列表与LINQ一个列表 [英] Merge multiple Lists into one List with LINQ

查看:133
本文介绍了合并多个列表与LINQ一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有多个列表合并到使用LINQ to有效地复制这种单一的列表中华而不实的方式?

 公共类RGB
{
    公众诠释红{搞定;组; }
    公众诠释绿色{搞定;组; }
    公众诠释蓝{搞定;组; }
    公共RGB(红INT,INT绿,蓝INT){红色=红色;绿色=绿色;蓝色=蓝色; }
}公共无效myFunction的()
{
    清单< INT>红色=新的List< INT> {0×00,×03,0×06,0×08,×09};
    清单< INT>绿色=新的List< INT> {0×00,0×05,0×06,0×07,的0x0A};
    清单< INT>蓝色=新的List< INT> {0×00,0×02,×03,0×05,×09};    清单<&RGB GT;颜色=新的List<&RGB GT;();    colors.Add(新RGB(红[0],绿[0],蓝色[0]));
    colors.Add(新RGB(红[1],绿[1],蓝色[1]));
    colors.Add(新RGB(红[2],绿[2],蓝[2]));
    colors.Add(新RGB(红[3],绿[3],蓝色[3]));
    colors.Add(新RGB(红[4],绿[4],蓝色[4]));
}

或者,由于分别列出来,它更有效地相继合并它们像下面这样。

 公共类RGB
{
    公众诠释红{搞定;组; }
    公众诠释绿色{搞定;组; }
    公众诠释蓝{搞定;组; }    公共RGB(红INT,INT绿,蓝INT){红色=红色;绿色=绿色;蓝色=蓝色; }
}公共无效myFunction的()
{
    清单< INT>红色=新的List< INT> {0×00,×03,0×06,0×08,×09};    清单<&RGB GT;颜色=新的List<&RGB GT;();    colors.Add(新RGB(红[0],0,0));
    colors.Add(新RGB(红[1],0,0));
    colors.Add(新RGB(红[2],0,0));
    colors.Add(新RGB(红[3],0,0));
    colors.Add(新RGB(红[4],0,0));    清单< INT>绿色=新的List< INT> {0×00,0×05,0×06,0×07,的0x0A};    颜色[0]。绿色=绿色[0];
    颜色[1]。绿色=绿色[1];
    颜色[2]。绿色=绿色[2];
    颜色[3]。绿色=绿色[3];
    颜色[4]。绿色=绿色[4];    清单< INT>蓝色=新的List< INT> {0×00,0×02,×03,0×05,×09};    颜色[0]。蓝色=蓝色[0];
    颜色[1]。蓝色=蓝色[1];
    颜色[2]。蓝色=蓝[2];
    颜色[3]。蓝色=蓝色[3];
    颜色[4]。蓝色=蓝色[4];
}


解决方案

你基本上试图拉上三个集合。如果只LINQ 邮编()方法支持的两个以上的同时压缩和解了。但很可惜,它仅支持只有两个在同一时间。但是我们可以让它工作:

  VAR红魔=新的List< INT> {0×00,×03,0×06,0×08,×09};
无功果岭=新的List< INT> {0×00,0×05,0×06,0×07,的0x0A};
VAR蓝色=新的List< INT> {0×00,0×02,×03,0×05,×09};VAR颜色=
    reds.Zip(greens.Zip(蓝调,Tuple.Create)
        (红,元组)=>新的RGB(红,tuple.Item1,tuple.Item2)
    )
    .ToList();

当然,这并不十分痛苦的写了一个扩展方法做到三个(或更多)。

 公共静态的IEnumerable< TResult>邮编及LT; TFirst,TSecond,TThird,TResult>(
    这IEnumerable的< TFirst>第一,
    IEnumerable的< TSecond>第二,
    IEnumerable的< TThird>第三,
    FUNC< TFirst,TSecond,TThird,TResult> resultSelector)
{
    使用(VAR enum1 = first.GetEnumerator())
    使用(VAR enum2 = second.GetEnumerator())
    使用(VAR enum3 = third.GetEnumerator())
    {
        而(enum1.MoveNext()及&放大器; enum2.MoveNext()及&放大器; enum3.MoveNext())
        {
            产量返回resultSelector(
                enum1.Current,
                enum2.Current,
                enum3.Current);
        }
    }
}

这使事情有更多更好的:

  VAR颜色=
    reds.Zip(绿色,蓝色,
        (红色,绿色,蓝色)= GT;新的RGB(红,绿,蓝)
    )
    .ToList();

Is there a slick way to merge multiple Lists into a single List using LINQ to effectively replicate this?

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

Or, since the lists arrive separately, its more effective to merge them sequentially like the following.

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

解决方案

You're essentially trying to zip up three collections. If only the LINQ Zip() method supported zipping up more than two simultaneously. But alas, it only supports only two at a time. But we can make it work:

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

Of course it's not terribly painful to write up an extension method to do three (or more).

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

This makes things a lot more nicer:

var colors =
    reds.Zip(greens, blues,
        (red, green, blue) => new RGB(red, green, blue)
    )
    .ToList();

这篇关于合并多个列表与LINQ一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆