Append a Lists Contents to Another List C#

Append a Lists Contents to another List C#

GlobalStrings.AddRange(localStrings);

Note: You cannot declare the list object using the interface (IList).

Documentation: List<T>.AddRange(IEnumerable<T>).

How to add List<> to a List<> in asp.net

Use List.AddRange(collection As IEnumerable(Of T)) method.

It allows you to append at the end of your list another collection/list.

Example:

List<string> initialList = new List<string>();
// Put whatever you want in the initial list
List<string> listToAdd = new List<string>();
// Put whatever you want in the second list
initialList.AddRange(listToAdd);

Add a list to another list in c#

Solved it using a Concat instead of AddRange

var schools = act.Concat(sch);  

Add elements from one list to another C#

Your question describes the List.AddRange method, which copies all the elements of its argument into the list object on which it is called.

As an example, the snippet

List<int> listA = Enumerable.Range(0, 10).ToList();
List<int> listB = Enumerable.Range(11, 10).ToList();
Console.WriteLine("Old listA: [{0}]", string.Join(", ", listA));
Console.WriteLine("Old listB: [{0}]", string.Join(", ", listB));
listA.AddRange(listB);
Console.WriteLine("New listA: [{0}]", string.Join(", ", listA));

prints

Old listA: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Old listB: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
New listA: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

showing that all the elements of listB were added to listA in the AddRange call.

Adding List<t>.add() another list

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

Adding List<t>.add() another list

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

Add List<T> to beginning of another List<T>

You can use List.InsertRange:

list.InsertRange(0, otherStrings);

Add lists to another list class

I've removed my former answer since you updated the question a lot.

The reason you don't see the property is because (as mentioned in a comment), the list only contains a "contract" to contain instances of the class A. Even if it also containes derrived classes (such as class B), it still isn't valid to access properties of B, because we can only ever be "certain" that the elements inside the list contain at least all the behavior and properties of A.

This is by design to ensure that we can't accidentally try accessing a property which doesn't exist for an instance of A during runtime.



Related Topics



Leave a reply



Submit