如果我有:

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包含四个字符串,我按加号按钮,以确保他们是不是所有的空值。

myList2 = getMeAnotherList();后,myList2包含六个串,我检查,以确保他们没有空......之后myList1.Concat(myList2); myList1只包含四根弦。为什么呢?

有帮助吗?

解决方案

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

其他提示

尝试这种情况:

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

的毗连返回一个IEnumerable 即两个列表放在一起,它不修改任何现有的列表。此外,由于它返回一个IEnumerable,如果你想将其分配给一个变量,它是名单,你必须呼吁了IEnumerable ToList()返回的。

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

它的正常工作我想是这样。如先前所述,的毗连返回一个新的序列并同时将所述结果列表时,它完美地完成这项工作。

它也值得注意的是,的毗连工作在恒定的时间和在常数存储器。 例如,下面的代码

        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

我知道这是旧的,但我来到这个帖子迅速思考的毗连将是我的答案。欧盟对我来说真是棒极了。请注意,它仅返回唯一值,但我们知道,我反正越来越唯一值这个解决方案为我工作。

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

拍摄看我实现its 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(); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top