tags:

views:

130

answers:

4

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

        List<VTSWeb.WorktimeViolation> list = new List<VTSWeb.WorktimeViolation>();
        list = VTSWeb.GetDailyWorktimeViolations(VehicleID);
        list.Add(VTSWeb.GetDailyWorktimeViolations(VehicleID2));
+7  A: 

Try using list.AddRange(VTSWeb.GetDailyWorktimeViolations(VehicleID2));

daRoBBie
also get rid of the List<VTSWeb.WorktimeViolation> list = new List<VTSWeb.WorktimeViolation>();statement as it's redundant.
WOPR
@WOPR: Is there a better method to do this? Gimme an example use..
HasanGursoy
+2  A: 
  1. Use Concat or Union extension methods. You have to make sure that you have this direction using System.Linq; in order to use LINQ extensions methods.

  2. Use the AddRange method.

Mendy
list.Concat(list); ?
HasanGursoy
Exactly. Make sure you have `using System.Linq;` direction.
Mendy
+1  A: 

Use .AddRange to append any Enumrable collection to the list.

Jonesie
+5  A: 

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)
Ando