添加列表< T>。新增()另一个列表 [英] Adding List<t>.add() another list
问题描述
我有一个的IEnumerable< TravelDetails>
,我尝试添加瓦莱斯在为
-loop到一个列表与LT; TravelDetails>
。我不断收到错误。
I have an IEnumerable<TravelDetails>
and I am trying to add the vales in the for
-loop to a List<TravelDetails>
. I keep getting the errors.
错误15参数1:无法从System.Collections.Generic.List'到'TrafficCore.DataObjects.TripDetails'C转换:\\ \\TrafficNew\TI 511-Web\Traffic 2.0\511Traffic\511Traffic\Models\DrivingTime.cs
Error 15 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'TrafficCore.DataObjects.TripDetails' C:\TrafficNew\TI 511-Web\Traffic 2.0\511Traffic\511Traffic\Models\DrivingTime.cs
我的代码是
List<TripDetails> tripDetailsCollection = new List<TripDetails>();
foreach (DrivingTimeRoute dtr in dtRoutes)
{
foreach (Trip trip in dtr.Trips)
{
foreach (TripPathLink tpl in trip.TripPathLinks)
{
tplCollection.Add(tpl);
}
IEnumerable<TripDetails> tripDetails = //long Linq-to-Sql here
List<TripDetails> td = tripDetails.ToList();
tripDetailsCollection.Add(td); // <<< Error here
}
}
有一个人帮助我。< BR>
谢谢,
爬完
Can some one help me with this.
Thanks,
Pawan
推荐答案
的 列表< T>。新增
增加一个元素。相反,使用 列表< T> .AddRange
,一>添加多个值
List<T>.Add
adds a single element. Instead, use List<T>.AddRange
to add multiple values.
此外,列表< T> .AddRange
需要一个的IEnumerable< T>
,所以你不需要 tripDetails
转换成列表< TripDetails>
,你可以直接通过它,例如:
Additionally, List<T>.AddRange
takes an IEnumerable<T>
, so you don't need to convert tripDetails
into a List<TripDetails>
, you can pass it directly, e.g.:
tripDetailsCollection.AddRange(tripDetails);
这篇关于添加列表< T>。新增()另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!