How do I transfer the items contained in one List
to another in C# without using foreach
?
-
3If you need a deep clone of the original list you will find the answer in this related question: stackoverflow.com/questions/222598/… – Dirk Vollmar Dec 23 '09 at 11:37
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:
List<Int32> copy = original.ToList();
-
17If the items are of type
MyClass
instead ofInteger
, does it copy the items too, or just reference them? – Pedro Moreira Jun 6 '14 at 14:14 -
7Not working with Non-primitive types. List<StudentClass> copy = new List<StudentClass>(lstStudentClass); – garish Apr 3 '15 at 10:59
-
4It works with all types, as long as
lstStudentClass
is anIEnumerable<StudentClass>
, it will work. If you experience otherwise you need to provide more information. – Lasse V. Karlsen Apr 3 '15 at 11:05 -
1
-
1You pass on a collection to the constructor of the list, and the constructor will then copy all the elements of that collection into the newly created list. – Lasse V. Karlsen Sep 13 '15 at 15:14
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.
-
6@mrmillsy: Well they do different things. My answer is focused on "I already have a list, and I want to copy things to it" – Jon Skeet Mar 1 '13 at 12:44
-
True. My question would probably be better suited to a new question anyway. Thanks for the reply though. – mrmillsy Mar 1 '13 at 12:47
-
2If you wanted to replace the contents of an existing list completely, you would call targetList.Clear() first. – Ibraheem Jul 20 '13 at 15:16
-
This is the correct answer as copying implies adding, not replacing. The OP asked for copying ITEMS not the entire collection. – John Stock Jan 16 at 3:22
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
List<string> lstNew = lstTest.GetRange(0, 3);
And this is if copying a single property to another list is needed:
targetList.AddRange(sourceList.Select(i => i.NeededProperty));
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;
}
Easy to map different set of list by linq without for loop
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();
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 :)
/* 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 );
}
-
2
-
mmm...ok well some of the suggestions above simply don't work in Vis 17 I have cleaned up the best answer provided...and hey presto...it works :) – Jim Stevens Aug 30 '17 at 14:31
public static List<string> GetClone(this List<string> source)
{
return source.Select(item => (string)item.Clone()).ToList();
}
-
1Code only answer tend to be considered low quality. Can you add an explanation as to how your answer solves the issue, and how this differs from existing answers. – Dijkgraaf Mar 1 at 3:05
Here another method but it is little worse compare to other.
List<int> i=original.Take(original.count).ToList();
-
25