Here is another example taken (slightly) directly from the MSDN page concerning Enumerable::SelectMany.
This example is in C++/CLI and appears to be missing from the list of examples.
// MSDN_bb534336_CPP.cpp : main project file.
// https://msdn.microsoft.com/en-us/library/vstudio/bb534336(v=vs.100).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;
public ref class PetOwner
{
private:
static List<String^>^ GetPets(PetOwner^ petOwner) { return petOwner->Pets; }
static IEnumerable<String^>^ GetListAsArray(List<String^>^ lst) { return lst; }
static IEnumerable<String^>^ GetArrayAsArray(IEnumerable<String^>^ arr) { return arr; }
public:
property String^ Name;
property List<String^>^ Pets;
PetOwner(String^ Name, List<String^>^ Pets)
{
this->Name = Name;
this->Pets = Pets;
}
static Func<PetOwner^, List<String^>^>^ getPets = gcnew Func<PetOwner^, List<String^>^>(GetPets);
static Func<List<String^>^, IEnumerable<String^>^>^ getListAsArray = gcnew Func<List<String^>^, IEnumerable<String^>^>(GetListAsArray);
static Func<IEnumerable<String^>^, IEnumerable<String^>^>^ getArrayAsArray = gcnew Func<IEnumerable<String^>^, IEnumerable<String^>^>(GetArrayAsArray);
};
static void SelectManyEx1()
{
array<PetOwner^, 1>^ petOwners =
{
gcnew PetOwner("Higa, Sidney", Enumerable::ToList<String^>(gcnew array < String^, 1 > { "Scruffy", "Sam" })),
gcnew PetOwner("Ashkenazi, Ronen", Enumerable::ToList<String^>(gcnew array < String^, 1 > { "Walker", "Sugar" })),
gcnew PetOwner("Price, Vernette", Enumerable::ToList<String^>(gcnew array < String^, 1 > { "Scratches", "Diesel" }))
};
// Query using SelectMany().
IEnumerable<List<String^>^>^ query3 =
Enumerable::Select<PetOwner^, List<String^>^>
( // extract List of String^ from PetOwners
petOwners, PetOwner::getPets
);
// This code shows how to use Select()
// instead of SelectMany().
Console::WriteLine("\nUsing Select():");
// Notice that two foreach loops are required to
// iterate through the results
// because the query returns a collection of arrays.
for each(List<String^>^ petList in query3)
{
for each(String^ pet in petList)
{
Console::WriteLine(pet);
}
Console::WriteLine();
}
IEnumerable<IEnumerable<String^>^>^ query2 =
Enumerable::Select<List<String^>^, IEnumerable<String^>^>
( // convert List of String^ to IEnumerable of String^
query3, PetOwner::getListAsArray
);
Console::WriteLine("Using SelectMany():");
IEnumerable<String^>^ query1 =
Enumerable::SelectMany<IEnumerable<String^>^, String^>
( // extract IENumerable of String^ from embedded IEnumerable
query2, PetOwner::getArrayAsArray
);
// Only one foreach loop is required to iterate
// through the results since it is a
// one-dimensional collection.
for each(String^ pet in query1)
{
Console::WriteLine(pet);
}
/*
This code produces the following output:
Using Select():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
Using SelectMany():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
*/
}