Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding List<t>.add() another list

Tags:

c#

.net

linq

People also ask

How to add another list to a list C#?

Use the AddRange() method to append a second list to an existing list. list1. AddRange(list2);

How to add a string list to a list in C#?

To add string values to a list in C#, use the Add() method.

How do I add one list to another list in VB net?

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.

Why does adding a new value to list <> overwrite previous values in the list <>?

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