Solid development

Don’t make excuses for code, let it speak for itself

  Home  |   Contact  |   Syndication    |   Login
  4 Posts | 0 Stories | 0 Comments | 0 Trackbacks

News

Article Categories

Archives

Post Categories

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);
}
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Friday, October 30, 2009 3:14 PM