C# 如何在没有 foreach 的情况下将项目从列表复制到列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1952185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I copy items from list to list without foreach?
提问by ratty
How do I transfer the items contained in one List
to another in C# without using foreach
?
如何List
在不使用 C# 的情况下将一个项目中包含的项目转移到另一个项目foreach
?
采纳答案by Lasse V. Karlsen
You could try this:
你可以试试这个:
List<Int32> copy = new List<Int32>(original);
or if you're using C# 3 and .NET 3.5, with Linq, you can do this:
或者,如果您使用的是 C# 3 和 .NET 3.5,则可以通过 Linq 执行以下操作:
List<Int32> copy = original.ToList();
回答by Jon Skeet
To add the contents of one list to another list which already exists, you can use:
要将一个列表的内容添加到另一个已经存在的列表中,您可以使用:
targetList.AddRange(sourceList);
If you're just wanting to create a new copy of the list, see Lasse's answer.
如果您只想创建列表的新副本,请参阅 Lasse 的回答。
回答by ratty
Here another method but it is little worse compare to other.
这是另一种方法,但与其他方法相比,它稍微差一些。
List<int> i=original.Take(original.count).ToList();
回答by Paras
For a list of elements
对于元素列表
List<string> lstTest = new List<string>();
lstTest.Add("test1");
lstTest.Add("test2");
lstTest.Add("test3");
lstTest.Add("test4");
lstTest.Add("test5");
lstTest.Add("test6");
If you want to copy all the elements
如果要复制所有元素
List<string> lstNew = new List<string>();
lstNew.AddRange(lstTest);
If you want to copy the first 3 elements
如果要复制前 3 个元素
List<string> lstNew = lstTest.GetRange(0, 3);
回答by garish
This method will create a copy of your list but your type should be serializable.
此方法将创建列表的副本,但您的类型应该是可序列化的。
Use:
用:
List<Student> lstStudent = db.Students.Where(s => s.DOB < DateTime.Now).ToList().CopyList();
Method:
方法:
public static List<T> CopyList<T>(this List<T> lst)
{
List<T> lstCopy = new List<T>();
foreach (var item in lst)
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, item);
stream.Position = 0;
lstCopy.Add((T)formatter.Deserialize(stream));
}
}
return lstCopy;
}
回答by usefulBee
And this is if copying a single property to another list is needed:
这是如果需要将单个属性复制到另一个列表:
targetList.AddRange(sourceList.Select(i => i.NeededProperty));
回答by Jim Stevens
OK this is working well From the suggestions above GetRange( ) does not work for me with a list as an argument...so sweetening things up a bit from posts above: ( thanks everyone :)
好的,这运行良好 从上面的建议 GetRange() 不适合我,以列表作为参数......所以从上面的帖子中得到一些甜头:(谢谢大家:)
/* Where __strBuf is a string list used as a dumping ground for data */
public List < string > pullStrLst( )
{
List < string > lst;
lst = __strBuf.GetRange( 0, __strBuf.Count );
__strBuf.Clear( );
return( lst );
}
回答by Rajeev
Easy to map different set of list by linq without for loop
无需 for 循环即可通过 linq 轻松映射不同的列表集
var List1= new List<Entities1>();
var List2= new List<Entities2>();
var List2 = List1.Select(p => new Entities2
{
EntityCode = p.EntityCode,
EntityId = p.EntityId,
EntityName = p.EntityName
}).ToList();