Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello everyone,
if i have List1, List2, List3, List4, List5, List6,
 
How can i copy all the values inside each list in a new List( ListTotal)
 
Thanks in advance
Posted 31-Jan-13 10:10am
z3ngew810
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can use AddRange[^] property if you don't want to use linq:
 
var l1 = new List<string> {"a", "b"};
var l2 = new List<string> {"c", "d"};
 
var res = new List<string>();
res.AddRange(l1);
res.AddRange(l2);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

the Union() extension method.
 
newList = list1.Union(list2).Union(list3);
 
I should point out that Union() filters out duplicates. If you want duplicates or know there are nonde, use Concat().
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can try this C# 2.0 solution (no LINQ is required here).
using System;
using System.Collections.Generic;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create sample lists
            var list1 = new List<string> { "A", "B", "C" };
            var list2 = new List<string> { "D", "E" };
            var list3 = new List<string> { "F", "G", "H" };
 
            // "Concatenate" lists into one enumeration
            var e4 = ConcatLists(list1, list2, list3);
 
            // Create one list from enumerable sequence
            var l4 = new List<string>(e4);
 
            // Print items
            foreach (var item in l4)
                Console.WriteLine(item);
 
            // Wait
            Console.ReadKey();
        }
 
        public static IEnumerable<T> ConcatLists<T>(params List<T>[] lists)
        {
            foreach (var list in lists)
            {
                foreach (var item in list)
                {
                    yield return item;
                }
            }
        }
    }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid