There’s one LINQ method that I always struggle in understanding: SelectMany.
It’s actually a pretty simple method, but somehow it does not get stuck in my head.
In simple words, SelectMany works on collections of items that you can use, in whichever way, to retrieve other items.
Let’s see an example using the dear old
for loop, and then we will replace it with
For this example, I’ve created a simple record type that represents an office. Each office has one or more phone numbers.
record Office(string Place, string PhoneNumbers);
Now, our company has a list of offices.
List<Office> myCompanyOffices = new List<Office> new Office("Turin", new string"011-1234567", "011-2345678", "011-34567890"), new Office("Rome", new string"031-4567", "031-5678", "031-890"), new Office("Dublin", new string"555-031-4567", "555-031-5678", "555-031-890"), ;
How can we retrieve the list of all phone numbers?
The most obvious way is to iterate over the collection with a
for or a
List<string> allPhoneNumbers = new List<string>(); foreach (var office in myCompanyOffices) allPhoneNumbers.AddRange(office.PhoneNumbers);
Nothing fancy: we use
AddRange instead of
Addjust to avoid another inner loop.
You can do the same thing in a single line using LINQ’s
List<string> allPhoneNumbers = myCompanyOffices.SelectMany(b => b.PhoneNumbers).ToList();
This method aggregates all the PhoneNumbers elements in an
IEnumerable<string> instance (but then we need to call
ToList to convert it).
Of course, always check that the
PhoneNumbers list is not
nullotherwise it will throw an
The simplest way is by using the
allPhoneNumbers = myCompanyOffices.SelectMany(b => b.PhoneNumbers ?? Enumerable.Empty<string>()).ToList();
Easy, right? I do not have anything more to add!