How to add List<> to a List<> in asp.net

HasanG picture HasanG · Mar 3, 2010 · Viewed 256.8k times · Source

Is there a short way to add List<> to List<> instead of looping in result and add new result one by one?

var list = GetViolations(VehicleID);
var list2 = GetViolations(VehicleID2);

list.Add(list2);

Answer

Ando picture Ando · Mar 3, 2010

Use List.AddRange(collection As IEnumerable(Of T)) method.

It allows you to append at the end of your list another collection/list.

Example:

List<string> initialList = new List<string>();
// Put whatever you want in the initial list
List<string> listToAdd = new List<string>();
// Put whatever you want in the second list
initialList.AddRange(listToAdd);