开发者

How to add List<> to a List<> in asp.net [duplicate]

开发者 https://www.devze.com 2022-12-21 15:29 出处:网络
This question already has answers here: Append a 开发者_如何学PythonLists Contents to another List C#
This question already has answers here: Append a 开发者_如何学PythonLists Contents to another List C# (7 answers) Closed 5 years ago.

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


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


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


  1. Use Concat or Union extension methods. You have to make sure that you have this declaration using System.Linq; in order to use LINQ extensions methods.

  2. Use the AddRange method.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消