你如何在 C# 中连接列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1042219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:49:06  来源:igfitidea点击:

How do you concatenate Lists in C#?

c#arrayslistconcatenation

提问by Matt

If I have:

如果我有:

List<string> myList1;
List<string> myList2;

myList1 = getMeAList();
// Checked myList1, it contains 4 strings

myList2 = getMeAnotherList();
// Checked myList2, it contains 6 strings

myList1.Concat(myList2);
// Checked mylist1, it contains 4 strings... why?

I ran code similar to this in Visual Studio 2008 and set break points after each execution. After myList1 = getMeAList();, myList1contains four strings, and I pressed the plus button to make sure they weren't all nulls.

我在 Visual Studio 2008 中运行了与此类似的代码,并在每次执行后设置断点。After myList1 = getMeAList();,myList1包含四个字符串,我按下加号按钮以确保它们不是全为空值。

After myList2 = getMeAnotherList();, myList2contains six strings, and I checked to make sure they weren't null... After myList1.Concat(myList2);myList1 contained only four strings. Why is that?

After myList2 = getMeAnotherList();,myList2包含六个字符串,我检查以确保它们不为空...之后myList1.Concat(myList2);myList1 仅包含四个字符串。这是为什么?

采纳答案by John Kugelman

Concatreturns a new sequence without modifying the original list. Try myList1.AddRange(myList2).

Concat不修改原始列表的情况下返回一个新序列。试试myList1.AddRange(myList2)

回答by Jonathan Rupp

Try this:

尝试这个:

myList1 = myList1.Concat(myList2).ToList();

Concatreturns an IEnumerable<T> that is the two lists put together, it doesn't modify either existing list. Also, since it returns an IEnumerable, if you want to assign it to a variable that is List<T>, you'll have to call ToList() on the IEnumerable<T> that is returned.

Concat返回一个 IEnumerable<T> ,它是两个列表放在一起,它不会修改任何一个现有列表。此外,由于它返回一个 IEnumerable,如果要将其分配给 List<T> 变量,则必须对返回的 IEnumerable<T> 调用 ToList()。

回答by Balasubramani M

targetList = list1.Concat(list2).ToList();

It's working fine I think so. As previously said, Concat returns a new sequence and while converting the result to List, it does the job perfectly.

我认为它工作正常。如前所述,Concat 返回一个新序列,在将结果转换为 List 的同时,它完美地完成了这项工作。

回答by Dmitry Andrievsky

It also worth noting that Concat works in constant time and in constant memory. For example, the following code

还值得注意的是,Concat 在恒定时间和恒定内存中工作。例如,下面的代码

        long boundary = 60000000;
        for (long i = 0; i < boundary; i++)
        {
            list1.Add(i);
            list2.Add(i);
        }
        var listConcat = list1.Concat(list2);
        var list = listConcat.ToList();
        list1.AddRange(list2);

gives the following timing/memory metrics:

给出以下时序/内存指标:

After lists filled mem used: 1048730 KB
concat two enumerables: 00:00:00.0023309 mem used: 1048730 KB
convert concat to list: 00:00:03.7430633 mem used: 2097307 KB
list1.AddRange(list2) : 00:00:00.8439870 mem used: 2621595 KB

回答by Esaith

I know this is old but I came upon this post quickly thinking Concat would be my answer. Union worked great for me. Note, it returns only unique values but knowing that I was getting unique values anyway this solution worked for me.

我知道这是旧的,但我很快就想到了这篇文章,认为 Concat 将是我的答案。联盟对我很有用。请注意,它只返回唯一值,但知道我得到了唯一值,无论如何这个解决方案对我有用。

namespace TestProject
{
    public partial class Form1 :Form
    {
        public Form1()
        {
            InitializeComponent();

            List<string> FirstList = new List<string>();
            FirstList.Add("1234");
            FirstList.Add("4567");

            // In my code, I know I would not have this here but I put it in as a demonstration that it will not be in the secondList twice
            FirstList.Add("Three");  

            List<string> secondList = GetList(FirstList);            
            foreach (string item in secondList)
                Console.WriteLine(item);
        }

        private List<String> GetList(List<string> SortBy)
        {
            List<string> list = new List<string>();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");

            list = list.Union(SortBy).ToList();

            return list;
        }
    }
}

The output is:

输出是:

One
Two
Three
1234
4567

回答by Basheer AL-MOMANI

Take a look at my implementation. It's safe from null lists.

看看我的实现。从空列表中它是安全的。

 IList<string> all= new List<string>();

 if (letterForm.SecretaryPhone!=null)// first list may be null
     all=all.Concat(letterForm.SecretaryPhone).ToList();

 if (letterForm.EmployeePhone != null)// second list may be null
     all= all.Concat(letterForm.EmployeePhone).ToList(); 

 if (letterForm.DepartmentManagerName != null) // this is not list (its just string variable) so wrap it inside list then concat it 
     all = all.Concat(new []{letterForm.DepartmentManagerPhone}).ToList();