Adding List<t>.add() another list

C#.NetLinq

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPawanView Question on Stackoverflow
Solution 1 - C#Chris SchmichView Answer on Stackoverflow