locked
Copy List<x> to List<y> RRS feed

  • Question

  • how to copy list<x> to another list<y>

    class x and class y are the same classes with different names.

    Friday, April 30, 2010 12:09 PM

Answers

  • I presume by saying "class x and class y are the same classes with different names" you mean both classes have same properties method etc.. but their clann name is differrent, if yes, why is that so is different question.

    Let me try to give u a solution , use linq

    List<x> xlist 
    
    //populate xlist
    
    List<y> ylist = (from xItem in xlist 
               select new y{prop1 = xItem.prop1......}).ToList()
    

               



    Please do not forgot to mark it as an answer if it is. Thanks Gags
    • Proposed as answer by HarisM Friday, April 30, 2010 12:54 PM
    • Marked as answer by Chao Kuo Friday, May 7, 2010 9:28 AM
    Friday, April 30, 2010 12:27 PM
  • If you prefer deep copy here is the code snippet to do it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace DynamicSamples
    {
      public class DeepCopyList 
      {
        public static void Main(string[] args)
        {
          List<SomeType> list1 = new List<SomeType>();
          List<SomeType> list2 = new List<SomeType>();
    
          list1.Add(new SomeType { SomeIntegerProperty = 1, SomeStringProperty = "H1" });
          list1.Add(new SomeType { SomeIntegerProperty = 2, SomeStringProperty = "H2" });
    
          // This will copy all the items from list 1 to list 2
          list1.ForEach(i => list2.Add(i.Clone() as SomeType));
    
        }
      }
    
      [Serializable]
      public class SomeType : ICloneable
      {
        public string SomeStringProperty { get; set; }
        public int SomeIntegerProperty { get; set; }
    
        // deep copy in separeate memory space
    
        public object Clone()
        {
    
          MemoryStream ms = new MemoryStream();
    
          BinaryFormatter bf = new BinaryFormatter();
    
          bf.Serialize(ms, this);
    
          ms.Position = 0;
    
          object obj = bf.Deserialize(ms);
    
          ms.Close();
    
          return obj;
    
        }
    
    
      }
    }
    
    • Proposed as answer by Chao Kuo Tuesday, May 4, 2010 10:07 AM
    • Marked as answer by Chao Kuo Friday, May 7, 2010 9:28 AM
    Friday, April 30, 2010 9:53 PM

All replies

  • Hi

    I think this will work

          List<string> t = new List<string>(); //original
          List<string> t2 = new List<string>(t.ToArray()); // copy of t
    Regards

    Friday, April 30, 2010 12:13 PM
  • I presume by saying "class x and class y are the same classes with different names" you mean both classes have same properties method etc.. but their clann name is differrent, if yes, why is that so is different question.

    Let me try to give u a solution , use linq

    List<x> xlist 
    
    //populate xlist
    
    List<y> ylist = (from xItem in xlist 
               select new y{prop1 = xItem.prop1......}).ToList()
    

               



    Please do not forgot to mark it as an answer if it is. Thanks Gags
    • Proposed as answer by HarisM Friday, April 30, 2010 12:54 PM
    • Marked as answer by Chao Kuo Friday, May 7, 2010 9:28 AM
    Friday, April 30, 2010 12:27 PM
  • Sorry i misundetood the question if it is so as Gags stated, then the linq solution is a nice one. I would also use linq. Forget my previous answer, it's a inconsiderate one.
    Friday, April 30, 2010 12:32 PM
  • @ helvatica

    my list is a generic one with x and y are the classes.

    yes ur solution is fine to the extent if the two lists are of string type,but mine is a generic list

     

     

     

     

    Friday, April 30, 2010 12:37 PM
  • @gags

    first of all thanks for sharing ur view.

     

    i dont have any knowldg on linq so i havent gone through ur solution.

     

    googled and find some suggesstions like IEnumerator....

    ICloneable for copying of list  but with same class.

     

    Friday, April 30, 2010 12:50 PM
  • It is a one liner using LINQ.

          List<string> list1 = new List<string>();
          List<string> list2 = new List<string>();
    
          // This will copy all the items from list 1 to list 2
          list1.ForEach(i => list2.Add(i));
    

     

    Friday, April 30, 2010 4:13 PM
  • If you prefer deep copy here is the code snippet to do it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace DynamicSamples
    {
      public class DeepCopyList 
      {
        public static void Main(string[] args)
        {
          List<SomeType> list1 = new List<SomeType>();
          List<SomeType> list2 = new List<SomeType>();
    
          list1.Add(new SomeType { SomeIntegerProperty = 1, SomeStringProperty = "H1" });
          list1.Add(new SomeType { SomeIntegerProperty = 2, SomeStringProperty = "H2" });
    
          // This will copy all the items from list 1 to list 2
          list1.ForEach(i => list2.Add(i.Clone() as SomeType));
    
        }
      }
    
      [Serializable]
      public class SomeType : ICloneable
      {
        public string SomeStringProperty { get; set; }
        public int SomeIntegerProperty { get; set; }
    
        // deep copy in separeate memory space
    
        public object Clone()
        {
    
          MemoryStream ms = new MemoryStream();
    
          BinaryFormatter bf = new BinaryFormatter();
    
          bf.Serialize(ms, this);
    
          ms.Position = 0;
    
          object obj = bf.Deserialize(ms);
    
          ms.Close();
    
          return obj;
    
        }
    
    
      }
    }
    
    • Proposed as answer by Chao Kuo Tuesday, May 4, 2010 10:07 AM
    • Marked as answer by Chao Kuo Friday, May 7, 2010 9:28 AM
    Friday, April 30, 2010 9:53 PM
  • Hi,

    Here is the right way to do what you want :

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    namespace Delegates
    {
      class X
      {
        public int Id { get; set; }
        public string Name { get; set; }
      }
    
      class Y
      {
        public int Id { get; set; }
        public string Name { get; set; }
      }
    
      class Program
      {
        static void Main(string[] args)
        {
          List<X> x = new List<X>();
          for (int i = 0; i < 100; i++)
            x.Add(new X { Id = i, Name = string.Format("x_{0}", i.ToString()) });
          // copy x to y
          List<Y> y = new List<Y>(x.ConvertAll<Y>(e => { return new Y { Id = e.Id, Name = e.Name }; }));
          Debug.Assert(x.Count == y.Count);
        }
    
      }
    }

    Best Regards,

    Sunday, May 2, 2010 6:34 PM
  •  List<string> list1 = new List<string>();
       List<string> list2 = new List<string>();
    
       // This will copy all the items from list 1 to list 2
       list1.ForEach(i => list2.Add(i));
    
    This will copy by reference. I want to copy by value. Is there any other way except deep copy?
    Wednesday, June 22, 2011 3:53 PM
  • Just ran across this.  Do this:

     

    var source = new List<string>();

    // populate...

    var copy = source.Select(x=>string.Copy(x));

     

    Have fun...


    WNC
    Tuesday, October 11, 2011 6:23 PM