如何在C#中连接列表?
如果我有:如何在C#中连接列表?,c#,arrays,list,concatenation,C#,Arrays,List,Concatenation,如果我有: 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
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?
列表myList1;
清单2;
myList1=getMeAList();
//选中myList1,它包含4个字符串
myList2=getMeAnotherList();
//选中myList2,它包含6个字符串
myList1.Concat(myList2);
//选中mylist1,它包含4个字符串。。。为什么?
我在VisualStudio2008中运行了类似的代码,并在每次执行后设置断点。在myList1=getMeAList()之后代码>,myList1
包含四个字符串,我按下加号按钮以确保它们不是全部为空
在myList2=getMeAnotherList()之后代码>,myList2
包含六个字符串,我检查了它们是否为空。。。在myList1.Concat(myList2)之后代码>myList1仅包含四个字符串。为什么会这样?试试这个:
myList1 = myList1.Concat(myList2).ToList();
返回一个IEnumerable,该IEnumerable是两个列表的组合,它不修改任何一个现有列表。此外,由于它返回一个IEnumerable,如果要将其分配给一个列表变量,则必须对返回的IEnumerable调用ToList()。Concat
返回一个新序列。请尝试myList1.AddRange(myList2)
我想它很好用。如前所述,Concat返回一个新序列,在将结果转换为列表时,它可以完美地完成这项工作。值得注意的是,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);
我知道这已经很老了,但我很快发现了这篇文章,我想康卡特应该是我的答案。工会对我很有用。注意,它只返回唯一的值,但知道无论如何这个解决方案对我来说都是唯一的
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;
}
}
}
看看我的实现。它不受空列表的影响
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();
IList all=new List();
if(letterForm.SecretaryPhone!=null)//第一个列表可能为null
all=all.Concat(letterForm.SecretaryPhone.ToList();
if(letterForm.EmployeePhone!=null)//第二个列表可能为null
all=all.Concat(letterForm.EmployeePhone.ToList();
如果(letterForm.DepartmentManagerName!=null)//这不是列表(它只是字符串变量),所以将其包装在列表中,然后对其进行压缩
all=all.Concat(新[]{letterForm.DepartmentManagerPhone}).ToList();
现在我重读了这个问题,.AddRange()听起来确实像OP真正想要的。@Kartiikeya如果它说参数无效,你就没有System.Linq的using语句,或者其中一个不是IEnumerable
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
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();