Ahmad Karimi
Ahmad Karimi

Reputation: 27

Combination the 2 Generic List

I have 2 Generic List:

List<Student> obj1 = new List<Student>();
List<Student> obj2 = new List<Student>();

I want Combination obj1 and obj2: obj1+obj2.

EX:

obj1:


1          remi         sistan

2          shaki        sistan

obj2:


3          nani         shahin

4          tina         shahin

obj1 + obj2:


1          remi         sistan

2          shaki        sistan

3          nani         shahin

4          tina         shahin

Upvotes: 0

Views: 97

Answers (5)

Tamir
Tamir

Reputation: 3901

you can use the Concatinate extention method that will return a new enumerable, or simply add the list2 into list1 like this:

obj1.AddRange(obj2);

Upvotes: 0

NPSF3000
NPSF3000

Reputation: 2451

Easy.

var list1 = Enumerable.Range(0, 10).ToList();
var list2 = Enumerable.Range(10, 10).ToList();
var list3 = list1.Concat(list2).ToList();  //uses System.Linq

You can also do:

var list1 = Enumerable.Range(0, 10).ToList();
var list2 = Enumerable.Range(10, 10).ToList();
list1.AddRange(list2);

Upvotes: 4

npinti
npinti

Reputation: 52185

You can use the .concat(List list) method like so: List<Student> con = obj1.Concat(obj2).ToList();

Upvotes: 0

Tigran
Tigran

Reputation: 62248

obj1.AddRange(obj2) should be enough for you.

Make use of AddRange method.

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

How about we do

List<Student> newobj = obj1.Concat(obj2).ToList();

For this you need to include namespace System.Linq

Upvotes: 1

Related Questions