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 :

Image

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

Image

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:

Image

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.

 

 

Advertisements

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 http://blog.michaelhidalgo.info/
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s