将多个列表复制到1个列表中 [英] copy multiple list into 1 list

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

问题描述

大家好,

如果我有List1,List2,List3,List4,List5,List6,



如何全部复制新列表中每个列表中的值(ListTotal)



提前谢谢

Hello everyone,
if i have List1, List2, List3, List4, List5, List6,

How can i copy all the values inside each list in a new List( ListTotal)

Thanks in advance

推荐答案

你可以使用 AddRange [ ^ ]如果您不想使用linq属性:



You can use AddRange[^] property if you don''t want to use linq:

var l1 = new List<string> {"a", "b"};
var l2 = new List<string> {"c", "d"};

var res = new List<string>();
res.AddRange(l1);
res.AddRange(l2);


Union()扩展方法。



newList = list1.Union(list2).Union(list3);



我应该指出Union()过滤掉重复项。如果你想要重复或知道有nonde,请使用Concat()。
the Union() extension method.

newList = list1.Union(list2).Union(list3);

I should point out that Union() filters out duplicates. If you want duplicates or know there are nonde, use Concat().


你可以试试这个 C#2.0 解决方案(这里不需要LINQ)。 />
You can try this C# 2.0 solution (no LINQ is required here).
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create sample lists
            var list1 = new List<string> { "A", "B", "C" };
            var list2 = new List<string> { "D", "E" };
            var list3 = new List<string> { "F", "G", "H" };

            // "Concatenate" lists into one enumeration
            var e4 = ConcatLists(list1, list2, list3);

            // Create one list from enumerable sequence
            var l4 = new List<string>(e4);

            // Print items
            foreach (var item in l4)
                Console.WriteLine(item);

            // Wait
            Console.ReadKey();
        }

        public static IEnumerable<T> ConcatLists<T>(params List<T>[] lists)
        {
            foreach (var list in lists)
            {
                foreach (var item in list)
                {
                    yield return item;
                }
            }
        }
    }
}


这篇关于将多个列表复制到1个列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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