How do you concatenate Lists in C#?

前端 未结 6 1956
闹比i
闹比i 2020-12-23 13:09

If I have:

List myList1;
List myList2;

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

myList2 = getMeAnother         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-23 13:35

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

     IList all= new List();
    
     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();
    

提交回复
热议问题