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 11:10am
z3ngew813
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)

  Print Answers RSS
0 OriginalGriff 455
1 Sergey Alexandrovich Kryukov 312
2 DamithSL 179
3 King Fisher 179
4 Peter Leow 154
0 OriginalGriff 240
1 Maciej Los 95
2 Sascha Lefévre 85
3 King Fisher 70
4 F-ES Sitecore 55


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100