Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Select(<selector>) method

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.

Wow! That's a mouthful! Let's try it this way. Select() takes each source element, transforms it, and returns a sequence of the transformed values. Better?

NOTE: Here we see LINQ showing its SQL roots again. The Select() method is named based on SQL syntax, but acts like the map() function in Java, JavaScript, Python, etc.

The selector method should take a single parameter of type T and should return a value of type U. The result sequence will have the same number of elements as the source sequence and each element will be of type U.

IEnumerable<string> strings = new List<string> { "one", "two", "three", "four" };
// Will return { 3, 3, 5, 4 }
IEnumerable<int> result = strings.Select(str => str.Length);
IEnumerable<Point> points = new List<Point>
    new Point(0, 0),
    new Point(1, 1),
    new Point(2, 0)
// Will return { "(0, 0)", "(1, 1)", "(2, 0)" }
IEnumerable<string> result = points.Select(pt => $"({pt.X}, {pt.Y})");


In this exercise, try to return a sequence of strings that consist of the provided names in the form: "<Last>, <First>".

Select Exercise
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content