Skip to content

How to use LINQ in C# for lists

How to use LINQ in C# for lists
Become a developer with our complete learning paths
Become a developer with our complete learning paths

LINQ in C#: How to use LINQ in C# for list. Create, filter, sort, and group.

LINQ, which stands for Language Integrated Query, is a powerful feature in C# that allows developers to work with lists easily. In this article, we will create a small project demonstrating the power of LINQ, how to use LINQ in C# for lists, and how it can help us filter, sort, and group them.

Creating the Project

  • Open Visual Studio
  • Select > Create new project

How to use LINQ in C# for lists

  • Select “Console App” from the list of templates.

How to use LINQ in C# for lists

  • Give your project a name, click “Next”, then select the latest NET version and click “Create”

How to use LINQ in C# for lists

  • This will create a new project with a file called “Program.cs” . This is where we will be writing all the code for our project. And you are ready to go! 

 

How to use LINQ in C# for lists

Let us create a list of integers we will work with. Add the following code:

This creates a new list called “numbers” and assigns values 2, 4, 6, 8, and 10. These values can be replaced with any other values of your choice.

Using LINQ to Filter the List

In this step, we will use LINQ to filter our list of integers and create a new list containing only the even numbers. Add the following code:

This line of code uses a LINQ method called “Where” to filter the “numbers” list and create a new list called “evenNumbers” that contains only the even numbers.

  • The “Where” method takes a lambda expression as an argument. A lambda expression is a simple way to write a function. In this case, the function takes an integer “n” and returns true if the remainder of “n” when divided by 2 is 0, meaning if it’s an even number. Otherwise, it returns false. 
  • The “ToList()” method is used to convert the filtered result into a new list.

Using LINQ to Sort the List

Now, we will use LINQ to sort our list of even numbers in ascending order. Add the following code:

This line of code uses a LINQ method called “OrderBy” to sort the “evenNumbers” list in ascending order.

  • The “OrderBy” method takes a lambda expression as an argument, in this case, it takes an integer “n” and returns “n” itself. This means that the list will be sorted according to the value of each number.
  • The “ToList()” method is used to convert the sorted result into a new list.

Using LINQ to Group the List

Next, we will use LINQ to group our list of even numbers by their value modulo 4. Add the following code:

This line of code uses a LINQ method called “GroupBy” to group the “evenNumbers” list according to the remainder of each number when divided by 4.

  • The “GroupBy” method takes a lambda expression as an argument, in this case, it takes an integer “n” and returns the remainder of “n” when divided by 4. This means that the numbers will be grouped by their remainder.

The result is stored in a variable called “groups” which is an IEnumerable of IGrouping, you can think of it as a list of lists.

Displaying the Results

Lastly, we want to find out if our efforts have worked, so we will now display the results of our LINQ operations on the console. For that, add the following code:

This code uses two “foreach” loops to iterate over the “groups” variable and display the results on the console.

  • The first loop iterates over each group, and the second loop iterates over the numbers in each group.
  • Each group’s “Key” property represents the remainder that the numbers in that group have when divided by 4.

Run the project

Press the “Start” button or use the shortcut “F5” to run the program. You should see the program’s output on the console, displaying the filtered, sorted, and grouped even numbers and their remainders when divided by 4.

How to use LINQ in C# for lists

CONCLUSION: How to use LINQ in C# for lists

That’s it! You’ve successfully created a program demonstrating LINQ’s power in C# and how it can help you easily filter, sort, and group lists. Feel free to experiment with different values and try out different LINQ methods to see what they can do.

If you’re new to C# and collections, it’s always a good idea to start with basic examples and then build on them as you gain more experience. And don’t forget that when in doubt, you can always refer to this article to help you understand how to use LINQ in C# for lists.

If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE which also covers test-driven development and C# software architecture.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide