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

推荐答案

List<T>.Add adds a single element. Instead, use List<T>.AddRange to add multiple values.

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

.net相关问答推荐

ECS服务无法从Cognito获取配置

保存时不保留 XML 格式

如何在 C# 中将 HTTP 发布请求发送到另一个 API?

无法通过构建目标访问 dotnet 的环境变量

如何在选项卡中 Select Winforms NumericUpDown 中的所有文本?

为什么 C# 不允许像 C++ 这样的非成员函数

比较 C# 中的字符串和对象

如何在 C# 中创建 Word 文档?

多个等待与 Task.WaitAll - 等效?

使用只读属性或方法?

公钥令牌的作用是什么?

如何保护我的 .NET 程序集免受反编译?

如何仅在需要时提升权限?

绑定到不在列表中的值的可编辑组合框

为什么 Roslyn 中有异步状态机类(而不是 struct )?

嵌套的 Try/Catch 块是个坏主意吗?

如何使用通用扩展方法中的字符串列名在 IQueryable 上应用 OrderBy?

是否可以判断对象是否已附加到实体框架中的数据上下文?

如何卸载Microsoft .NET Core 1.0.0 RC2 - VS 2015 Tooling Preview 1?

NServiceBus 与 MassTransit