C# 如何在asp.net中将List<>添加到List<>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2369673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:49:48  来源:igfitidea点击:

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

c#asp.netlist

提问by HasanG

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

是否有一种简短的方法可以将 List<> 添加到 List<> 而不是在结果中循环并一个一个地添加新结果?

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

list.Add(list2);

采纳答案by Ando

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

使用List.AddRange(collection As IEnumerable(Of T))方法。

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

回答by Rob Tillie

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

尝试使用 list.AddRange(VTSWeb.GetDailyWorktimeViolations(VehicleID2));

回答by Fitzchak Yitzchaki

  1. Use Concator Unionextension methods. You have to make sure that you have this direction using System.Linq;in order to use LINQ extensions methods.

  2. Use the AddRangemethod.

  1. 使用ConcatUnion扩展方法。你必须确保你有这个方向using System.Linq;才能使用 LINQ 扩展方法。

  2. 使用AddRange方法。

回答by Jonesie

Use .AddRangeto append any Enumrable collection to the list.

使用.AddRange任何Enumrable集合追加到列表中。