Use the AddRange() method to append a second list to an existing list. list1. AddRange(list2);
To add string values to a list in C#, use the Add() method.
Concat produces a new sequence(well, actually is doesn't produce it immediately due to Concat 's deferred execution, it's more like a query) and you have to use ToList to create a new list from it. List. AddRange adds them to the same List so modifes the original one.
Essentially, you're setting a Tag's name to the first value in tagList and adding it to the collection, then you're changing that same Tag's name to the second value in tagList and adding it again to the collection. Your collection of Tags contains several references to the same Tag object!
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With