Getting Started with the LINQ Select Operator in C#

linq selectThe LINQ Select operator is a type of projection operator, which takes a collection of items as an input, performs some sort of projection on the items in the collection and returns the same number of elements for the output. The projection performed on the collection is specified by the lambda expression or the predicate that is passed to the Select operator.

To learn more about LINQ, check out this course

A Basic LINQ Select Example in C#

In the following example, the LINQ Select statement has been used to multiply all the elements in an integer array by 2 and return the projected collection:

int[] numbers = { 4, 2, 5, 3, 9, 5, 1, 7, 66, 44, 7, 5, 22, 15 };

IEnumerable<int> nummultiplied = numbers.Select(n => n * 2);

foreach (int n in nummultiplied)

{

Console.Write(n + " ");

}

 

In the above code snippet, an integer type array, named numbers has been instantiated with some random numbers. The Select operator operates on this numbers array and returns a collection in which every item is multiplied by two. The Select operator is applied in fluent syntax on the numbers array. The lambda expression “n => n * 2” passed to Select operator does the real trick here.

Using LINQ Select with string type collections

The LINQ Select operator can also be used to manipulate the string type items in a collection. For instance, the index of the item in the collection can be appended with the item name. This concept is demonstrated in the following example:

List<string> countries = new List<string> { "USA", "CANADA", "AUSTRALIA", "ENGLAND", "CHINA", "RUSSIA" };

IEnumerable<string> orderedcon = countries.Select((n, i) => i+"-" + n);

foreach (string country in orderedcon)

{

Console.WriteLine(country);

}

Console.ReadLine();

 

In the above example, a string type List collection, named countries, has been instantiated with some random country names. The LINQ Select operator has is applied on the countries collection. The lambda expression passed to the Select statement “n, i) => i+”-” + n”, contains two variables n and i. The first variable ‘n’ corresponds to the item and the second variable ‘i’ corresponds to the index of the item in the collection the expression “=> i+”-” + n” states that return the item index appended with item’s value separated by ‘-’. If the collection returned by this Select operator is printed on the console, it will have the following output.

0-USA
1-CANADA
2-AUSTRALIA
3-ENGLAND
4-CHINA
5-RUSSIA

Using LINQ Select to Change the Character Case of String Items

Similarly, when the LINQ Select operator is applied on string type collections, several string functions can be passed to the lambda expression of the corresponding select statement. For instance, all the elements in countries collection used in the last example can be converted to lowercase using following Select statement.

IEnumerable<string> orderedcon = countries.Select(n => n.ToLower());

 

This select operator returns an IEnumerable collection of a string type, which would contain all the items in the countries collection in lowercase.

Likewise, to convert all the items in the collection to uppercase, the following Select statement can be used:

Select(n => n.ToUpper());

 

Interested in learning more about C#? Take a course at Udemy.com.

Using LINQ Select to Find the Length of Each String Item

To find the length of all the items in string type collection, the LINQ Select statement can be used in the following manner:

List<string> countries = new List<string> { "USA", "CANADA", "AUSTRALIA", "ENGLAND", "CHINA", "RUSSIA" };

IEnumerable<int> counlengths = countries.Select(n => n.Length);

foreach (int country in counlengths)

{

Console.WriteLine(country);

}

Console.ReadLine();

 

The output of the above example contains the lengths of each individual country name printed on the console output.

Replacing substrings using LINQ Select in C#

The LINQ Select operator can be used to replace a substring in each item of the string type collection. This is also done through the lambda expression. The following example demonstrates this concept:

List<string> countries = new List<string> { "USA", "CANADA", "AUSTRALIA", "ENGLAND", "CHINA", "RUSSIA" };

IEnumerable<string> replacedcon = countries.Select(n => n.Replace("US", "EA"));

foreach (string country in replacedcon)

{

Console.WriteLine(country);

}

 

Implementing LINQ Select operator on custom types

Almost every LINQ operator can be implemented on custom types in a manner similar to how they are implemented on primitive types. Similarly, the LINQ select operator can also operate on custom types. Suppose there is a custom type class Person which is used to store name, age and date of birth of any person. The class looks like this:

class Person

{

public string name;

public int age;

public DateTime dob;

public Person(string name, int age, DateTime dob)

{

this.name = name;

this.age = age;

this.dob = dob;

}

}

 

In the main method, suppose 4 objects of the Person class have been instantiated and stored in a List collection of Person type. The Main method looks like this:

Person p1 = new Person("Adam", 22, new DateTime(1992, 2, 5));

Person p2 = new Person("Tori", 19, new DateTime(1995, 10, 6));

Person p3 = new Person("James", 29, new DateTime(1985, 11, 2));

Person p4 = new Person("Sofi", 24, new DateTime(1990, 4, 10));

List<Person> personlist = new List<Person> { p1, p2, p3, p4 };

 

To select only the values of the name variables of the Person objects in personlist collection, the LINQ Select operator can be used. The following example demonstrates this concept:

IEnumerable<string> orderednames = personlist.Select(n => n.name);

foreach (string name in orderednames)

{

Console.WriteLine(name);

}

 

In the above example, the Select operator is selecting only the name of the items in the personlist collection. This is done via lambda expression “n => n.name” which states that from the each item ‘n’ in the personlist collection, select ‘n.name’.

Chaining LINQ Select operator with other LINQ operators

The LINQ Select operator can be chained with other operators. For instance, to select all the names in the Person type items in the personlist collection, ordered by their length, the Select operator can be chained after the OrderBy operator as follows:

orderednames = personlist.OrderBy(n => n.name.Length).Select(n => n.name);

 

For more exciting .NET tutorials, take this course at Udemy.com.