【问题标题】:How do you concatenate Lists in C#?如何在 C# 中连接列表?
【发布时间】:2010-11-05 17:55:25
【问题描述】:

如果我有:

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 之后只包含四个字符串。这是为什么呢?

【问题讨论】:

    标签: c# arrays list concatenation


    【解决方案1】:

    Concat 返回一个新序列without modifying the original list。试试myList1.AddRange(myList2)

    【讨论】:

      【解决方案2】:

      试试这个:

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

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

      【讨论】:

      • 现在我重新阅读了这个问题,.AddRange() 听起来确实是 OP 真正想要的。
      • @Kartiikeya 如果它说参数无效,您没有 System.Linq 的 using 语句,或者其中一个不是 IEnumerable&lt;T&gt;
      【解决方案3】:
      targetList = list1.Concat(list2).ToList();
      

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

      【讨论】:

        【解决方案4】:

        同样值得注意的是,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
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              猜你喜欢
              • 2021-12-16
              • 1970-01-01
              • 2023-02-13
              • 2021-07-08
              • 1970-01-01
              • 2020-04-30
              • 1970-01-01
              • 2010-12-15
              相关资源
              最近更新 更多