LINQ: Combine elements in nested collection

Simple script to combine elements within nested list.

  1. static void Main(string[] args)
  2. {
  3.             List<List<string>> nestedList = new List<List<string>>();
  4.             nestedList.Add(new List<string>() { “a”, “b”, “c” });
  5.             nestedList.Add(new List<string>() { “d”, “e”, “f” });
  6.             List<string> all = (from n in nestedList
  7.                                 from l in n
  8.                                 select l).ToList();
  9.             all.ForEach(new Action<string>(delegate(string s) { Console.WriteLine(s); }));
  10.             Console.ReadLine();
  11. }

The output is “abcdef”