[英]how to copy a list to a new list, or retrieve list by value in c#
I noticed in c# there is a method for Lists: CopyTo -> that copies to arrays, is there a nicer way to copy to a new list?我注意到在 c# 中有一个列表方法:CopyTo -> 复制到数组,有没有更好的方法复制到新列表? problem is, I want to retrieve the list by value to be able to remove items before displaying them, i dont want the original list to be modified, that too doesnt seem to be easily attainable, any ideas?问题是,我想按值检索列表以便能够在显示之前删除项目,我不希望修改原始列表,这似乎也不容易实现,有什么想法吗?
List<MyType> copy = new List<MyType>(original);
我想按值检索列表,以便能够在显示之前删除项目,
var newlist = oldList.Where(<specify condition here>).ToList();
如果您使用的是 .NET 3.5,则生成的数组可以调用 ToList()。
只需创建一个新List
并使用适当的构造函数:
IList<Obj> newList = new List<Obj>(oldList);
I think this will work.我认为这会奏效。 Passing a list to the constructor of a new list.将列表传递给新列表的构造函数。
List<string> list1 = new List<string>();
List<string> list2 = new List<string>(list1);
您是否尝试过克隆 (Clone()) 每个项目并将克隆添加到新集合中?
It seems if you have a list of references, the list似乎如果你有一个参考列表,列表
List<Object> list2 = new List<Object>(list1);
does not work.不起作用。
This should solve your problem:这应该可以解决您的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.