添加 List.add() 另一个列表
我有一个 IEnumerablefor
循环中的值添加到 List
中。我不断收到错误。
错误 15 参数 1:无法从“System.Collections.Generic.List”转换为“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
}
}
有人可以帮我解决这个问题吗?
谢谢, 帕万
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.
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
My code is
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
}
}
Can some one help me with this.
Thanks,
Pawan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
List.Add
添加单一元素。相反,请使用List.AddRange
添加多个值。此外,
List.AddRange
采用IEnumerable
,因此您无需将tripDetails
转换为List
,直接传递即可,eg:List<T>.Add
adds a single element. Instead, useList<T>.AddRange
to add multiple values.Additionally,
List<T>.AddRange
takes anIEnumerable<T>
, so you don't need to converttripDetails
into aList<TripDetails>
, you can pass it directly, e.g.: