Search code examples
c#-4.0collections

Issue to Append two list


I have 2 List

like

Class A{
   int rollno{get;set;}
   string Name {get;set;}
}

List<A> a=new List<A>();
List<A> b=new List<A>();

After lot of research i am notable to append List b to List a. Thanks for any assistance.


Solution

  • List<A> finalList = new List<A>(a.Count + b.Count);
    finalList.AddRange(a);
    finalList.AddRange(b);
    

    Also, if you are using LINQ, you could use the Concat method, as suggested by @TimSchmelter. I didn't post this method earlier, because the AddRange method is faster.

    List<A> finalList = a.Concat(b).ToList();
    

    Note that you might not even need the ToList. If you're not going to be changing (removing/adding) data after the concatenation, then you might as well store it in an IEnumerable<A>:

    var finalCollection = a.Concat(b);