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