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


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Lambda Expressions Extension Method 7 - Select()

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

Lambda Expressions Extension Method 7 - Select()
 

Select() Extension Method

The Select() is a straight forward method.  It converts the argument from one type to another type.

For eg: If we need to convert a list of integers to string, we can achieve that using Select() as given below.

Similarly we can use value types to custom class conversions, to anonymous type conversions etc.

Parameters: Input type and the Transform Functions

Return Value: List of Items

Example

In the following example we are using a list of integers and the Select() method to convert each elements in the list.

Code

        private IList<int> _list = new List<int>();

 

        public InitializeList()

        {

            // Set data

            _list.Add(1);

            _list.Add(2);

            _list.Add(3);

            _list.Add(4);

            _list.Add(5);

            _list.Add(6);

        }

 

        private void SelectButton_Click(object sender, EventArgs e)

        {

            var result = _list.Select(a => a.ToString());

 

            foreach (string s in result)

                MessageBox.Show(s);

        }

 

 

 

 

Input

1

2

3

4

5

6

 

Output

 

1

2

3

4

5

6

(Converted to string)

 

Extension Method Snapshot

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


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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