2009-06-25 14 views
117

Se ho:Come si concatenano elenchi in C#?

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? 

ho corse codice simile a questo in Visual Studio 2008 e impostare punti di interruzione dopo ogni esecuzione. Dopo myList1 = getMeAList();, myList1 contiene quattro stringhe e ho premuto il pulsante più per accertarmi che non fossero tutti null.

Dopo myList2 = getMeAnotherList();, myList2 contiene sei corde, e ho controllato per assicurarsi che essi non erano nulla ... Dopo myList1.Concat(myList2); myList1 conteneva solo quattro corde. Perché?

+1

Cos'è questo Utilizzato per i punti di interruzione di business? –

risposta

66

Prova questa:

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

Concat restituisce un IEnumerable < T> che è le due liste messe insieme, non modifica né elenco esistente. Inoltre, poiché restituisce un oggetto IEnumerable, se si desidera assegnarlo a una variabile che è List < T>, sarà necessario chiamare ToList() su IEnumerable < T> che viene restituito.

+4

Ora che rileggo la domanda, .AddRange() suona come quello che l'OP vuole davvero. –

+0

dicendo che il metodo concat ha argomenti non validi ... –

+0

@Kartiikeya se sta dicendo che gli argomenti non sono validi, non hai un'istruzione using per System.Linq, o uno di loro non è un 'IEnumerable ' –

8

Concat non sta aggiornando myList1 restituisce un nuovo elenco contenente il numero myList1 e myList2 concatenati.

Utilizzare myList1.AddRange(myList2) invece.

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

Sta funzionando bene, credo di si. Come detto in precedenza, Concat restituisce una nuova sequenza e mentre converte il risultato in List, fa perfettamente il lavoro.

2

Vale anche la pena notare che Concat funziona in tempo costante e nella memoria costante. Per esempio, il seguente codice di

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

ha pronunciato la seguente tempistica metriche/memoria:

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 
1

So che questo è vecchio, ma mi sono imbattuto in questo post in fretta pensando Concat sarebbe la mia risposta. L'unione ha funzionato alla grande per me. Nota, restituisce solo valori univoci, ma sapendo che stavo ottenendo valori unici, comunque questa soluzione ha funzionato per me.

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; 
     } 
    } 
} 

L'output è:

One 
Two 
Three 
1234 
4567 
1

prendere un'occhiata al mio implementazione 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(); 
Problemi correlati