196

如果我有:

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?

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

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

4

6 回答 6

353

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

于 2009-06-25T04:45:24.800 回答
114

试试这个:

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

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

于 2009-06-25T04:44:59.137 回答
14
targetList = list1.Concat(list2).ToList();

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

于 2014-06-23T09:17:11.967 回答
5

还值得注意的是,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);

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

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
于 2014-11-22T01:13:16.553 回答
2

我知道这已经过时了,但我很快就想到了这篇文章,认为 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;
        }
    }
}

输出是:

One
Two
Three
1234
4567
于 2015-09-28T18:16:32.907 回答
2

看看我的实现。它不受空列表的影响。

 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();
于 2016-09-21T17:46:13.603 回答