Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 23, 2012 01:00 PM by matifnadeem
Participant
1520 Points
775 Posts
Nov 23, 2012 10:36 AM|LINK
I have one List<Customer> object named as "customers"
another list List<Customer> object named as "activeCustomers"
and one more list List<Customer> object named as "inactiveCustomers"
is there any way to add all inactiveCustomers and activeCustomers to a "customers" collection without iterating over each of them?
Contributor
2042 Points
345 Posts
Nov 23, 2012 10:44 AM|LINK
Hi, You can use AddRage method List<customer> activeCustomers; List<customer> inactiveCustomers; List<customer> customers; customers.AddRange(activeCustomers); customers.AddRange(inactiveCustomers);
4758 Points
1130 Posts
Nov 23, 2012 01:00 PM|LINK
nirman.doshi I have one List<Customer> object named as "customers" another list List<Customer> object named as "activeCustomers" and one more list List<Customer> object named as "inactiveCustomers" is there any way to add all inactiveCustomers and activeCustomers to a "customers" collection without iterating over each of them?
Hi nirman.doshi,
Use AddRange to add multiple collection list into one list. Modify it according to your requirement.
List<int> a = new List<int>(); a.Add(1); a.Add(2); a.Add(5); a.Add(6); // Contains: // 1 // 2 // 5 // 6 int[] b = new int[3]; b[0] = 7; b[1] = 6; b[2] = 7; // Contains: // 7 // 6 // 7 a.AddRange(b); // Contains: // 1 // 2 // 5 // 6 // 7 [added] // 6 [added] // 7 [added] foreach (int i in a) { Console.WriteLine(i); }
Cheers
nirman.doshi
Participant
1520 Points
775 Posts
How to copy multiple items in a List collection?
Nov 23, 2012 10:36 AM|LINK
I have one List<Customer> object named as "customers"
another list List<Customer> object named as "activeCustomers"
and one more list List<Customer> object named as "inactiveCustomers"
is there any way to add all inactiveCustomers and activeCustomers to a "customers" collection without iterating over each of them?
Software Developer
Vadodara, India
alankarp
Contributor
2042 Points
345 Posts
Re: How to copy multiple items in a List collection?
Nov 23, 2012 10:44 AM|LINK
Hi,
You can use AddRage method
List<customer> activeCustomers;
List<customer> inactiveCustomers;
List<customer> customers;
customers.AddRange(activeCustomers);
customers.AddRange(inactiveCustomers);
Profile
matifnadeem
Contributor
4758 Points
1130 Posts
Re: How to copy multiple items in a List collection?
Nov 23, 2012 01:00 PM|LINK
Hi nirman.doshi,
Use AddRange to add multiple collection list into one list. Modify it according to your requirement.
List<int> a = new List<int>(); a.Add(1); a.Add(2); a.Add(5); a.Add(6); // Contains: // 1 // 2 // 5 // 6 int[] b = new int[3]; b[0] = 7; b[1] = 6; b[2] = 7; // Contains: // 7 // 6 // 7 a.AddRange(b); // Contains: // 1 // 2 // 5 // 6 // 7 [added] // 6 [added] // 7 [added] foreach (int i in a) { Console.WriteLine(i); }Cheers
M Atif Nadeem
Mark as Answer if you got right thing
Read my blog | Follow me on LinkedIn