375

How do I transfer the items contained in one List to another in C# without using foreach?

yoozer8
  • 7,361
  • 7
  • 58
  • 93
ratty
  • 13,216
  • 29
  • 75
  • 108
  • 3
    If you need a deep clone of the original list you will find the answer in this related question: http://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c – Dirk Vollmar Dec 23 '09 at 11:37

10 Answers10

661

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();

I see that this answer is still getting upvotes. Well, here's a secret for ya: the above answer is still using a foreach. Please don't upvote this any further.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • 22
    If the items are of type `MyClass` instead of `Integer`, does it copy the items too, or just reference them? – Pedro Moreira Jun 06 '14 at 14:14
  • 9
    Not working with Non-primitive types. List copy = new List(lstStudentClass); – garish Apr 03 '15 at 10:59
  • 4
    It works with all types, as long as `lstStudentClass` is an `IEnumerable`, it will work. If you experience otherwise you need to provide more information. – Lasse V. Karlsen Apr 03 '15 at 11:05
  • 1
    what is the meaning of (original); at the end – Rahul Chaudhari Sep 13 '15 at 14:40
  • 1
    You 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
  • 3
    @PedroMoreira For complex object you can do: `origninal.Select(o => new Complex(o)).ToList()` to get a list of new objects, not copied references – Luke T O'Brien Jun 20 '17 at 16:25
  • 1
    @JohnStock: Nothing's being replaced here - it's not changing any existing list. It's just creating a *new* list with the items. What do you believe is being replaced? – Jon Skeet Jan 16 '21 at 07:07
205

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 the top answer.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 01 '13 at 12:44
  • True. My question would probably be better suited to a new question anyway. Thanks for the reply though. – mrmillsy Mar 01 '13 at 12:47
  • 3
    If 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 '21 at 03:22
44

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);
Martin Vrábel
  • 830
  • 1
  • 13
  • 36
Paras
  • 2,997
  • 6
  • 35
  • 46
  • 1
    This is the correct answer. `AddRange` does not specify it in the docs, that it copies or just adds objects by reference, but if you search the implementation, it uses `Array.Copy` (static) to copy the data. – Paul-Sebastian Manole Apr 26 '21 at 19:43
7

And this is if copying a single property to another list is needed:

targetList.AddRange(sourceList.Select(i => i.NeededProperty));
usefulBee
  • 9,250
  • 10
  • 51
  • 89
4

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;
    }
garish
  • 637
  • 12
  • 14
4

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();
Rajeev
  • 381
  • 3
  • 9
2

Adding to the top answers, if you want copies of "the objects in the list", then you can use Select and make the copies. (While the other answers make "a copy of a list", this answer makes "a list of copies").

Suppose your item has a Copy method:

List<MyObject> newList = oldList.Select(item => item.Copy()).ToList();

Or that you can create a new object from the previous one with a constructor:

List<MyObject> newList = oldList.Select(item => new MyObject(item)).ToList();   

The result of Select is an IEnumerable<MyObject> that you can also pass to AddRange for instance, if your goal is to add to an existing list.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
0
public static List<string> GetClone(this List<string> source)
{
    return source.Select(item => (string)item.Clone()).ToList();
}
  • 1
    Code 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 01 '21 at 03:05
-1

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 );
}
-9

Here another method but it is little worse compare to other.

List<int> i=original.Take(original.count).ToList();
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
ratty
  • 13,216
  • 29
  • 75
  • 108