.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Lambda Expressions Extension Method 9 - SelectMany()

Posted By:Jean Paul       Posted Date: May 12, 2011    Points: 200    Category:    URL: http://www.dotnetspark.com  

Lambda Expressions Extension Method 9 - SelectMany()
 

SelectMany Extension Method

The SelectMany() method is different from the Select() method.  The SelectMany() method is used to flatten the elements into a new IEnumerable sequence.  (where as Select() method is used to convert the type of element to another)

Consider you have a string list of "abc", "def" and "ghi".  We can use the SelectMany() method to combine all character into a single IEnumerable like "abcdefghi".

Parameters: Input type and Transformation Method

Return Value: List of Items

Note: There are 4 overloads for the SelectMany() method.  The above Parameters is meant for the first overloaded method which takes Func> as argument.

If we need to convert an integer list to array of

Code

        private void SelectManyButton_Click(object sender, EventArgs e)

        {

            IList<string> list = new List<string>();

            list.Add("abc");

            list.Add("def");

            list.Add("ghi");

 

            var result = _list.SelectMany(s => s.ToString().ToCharArray());

 

            foreach (char c in result)

                MessageBox.Show(c.ToString());

        }

 

Input

 

"abc"

"def"

"ghi"

 

Output

 

a

b

c

d

e

f

g

h

i

 

 

Extension Method Snapshot

You can find the snapshot of the SelectMany() method usage as provided by Visual Studio.



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend