Selecting values from Multiple Collections : Using SelectMany LINQ method

This time I wanted to  determine how many library items there were in two separate TeamMentor libraries : OWASP and Top Vulnerabilities.

I wanted to find an easy way to determine how many libraries were loaded in the jsTree control and after that be able to determine how many items were loaded (how many leafs were loaded).

Firstly, I created a single collection of Libraries using LINQ capabilities to merge subsets of data :


This is the way I used to be able to create a list of libraries :


Look for example that our assert verifies that 2 libraries were loaded. Considering that I do have a list of libraries, I can use the power of LINQ to determine how many library items (a.k.a nodes or leafs) were loaded. Since all the libraries are in the list, I can use SelectMany LINQ method which allow me to  find all the elements within a collection, therefore applied to our case , this would be the way achieve that:


As you can see in this code section, there is not need to do an iteration over the List elements because SelectMany returns all the elements from the libaries and returns that in a single value.




About Michael Hidalgo

Michael is a Software Developer Engineer based on San José, Costa Rica. He leads the OWASP Chapter from Costa Rica. You can take a look at my blog at
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s