Geeks With Blogs
Solid development Don’t make excuses for code, let it speak for itself
Sometimes a LINQ query or other method returns a nested collection but you just want one big collection instead. SelectMany is the answer, see the code below. It can off course also be combined with OrderBy or Distinct if you prefer.

List<List<double>> nestedList = new List<List<double>>
                                   {
                                      new List<double> { 1.2, 2.4 },
                                      new List<double> { 3.6, 4.8 }
                                   };

List<double> flatList = nestedList.SelectMany(doubleList => doubleList).ToList();
foreach (double item in flatList)
{
   Console.WriteLine(item);
}
Posted on Friday, October 30, 2009 3:14 PM LINQ , C# | Back to top


Comments on this post: Merging sub collections into one collection

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © martin | Powered by: GeeksWithBlogs.net