LINQ Ordering Simplified in .NET 7


LINQ now has a simplified ordering of values in .NET 7.

Let’s see in action in this article

Step 1

Open Visual Studio 2022.

I’m using Visual Studio 2022, version 17.4.1, for this demonstration; if you want.NET 7 support in Visual Studio, use this version or higher.

Select Create a new project.

Step 2

Search Console App 

Select  Console App (make sure you choose the Console App not Console App .Net Framework as it is a older version of .NET) 

Click “Next.”

Step 3

Provide the name in the project name field.

Click “Next.”

Step 4

In this step, you have to select the framework.

Select .NET 7.0

Click “Next.”

Step 5

Now, in this step, we are going to write the code to order the data.

As you can see, I have created a list of integer type that contains the weights for showing the demo.

List<int> weights = new() { 105, 40, 75, 60, 35, 90 }; 

Now look at the old way of sorting this data 

In .NET 6 and below we are sorting the list by writing the following line

var oldSortedWeights = weights.OrderBy(x => x);

In essence, what we are saying is to “OrderBy itself,” meaning that for an integer, there is only one thing to OrderBy, which is the actual integer itself. As a result, we are saying to “just OrderBy that,” which was the traditional method for sorting any numbers present in a list of numbers or even a list of strings because you are simply ordering the values.

So now lets look at the new way to sorted weights in ascending order

var newSortedWeights = weights.Order();

So let’s see if they both work similarly next. I have written additional code to print the values on the screen

Console.WriteLine("Old Method to Sort Data");
foreach (var weight in oldSortedWeights)
{
    Console.WriteLine(weight);
}
Console.WriteLine("");

Console.WriteLine("New Method to Sort Data");
foreach (var weight in newSortedWeights)
{
    Console.WriteLine(weight);
}

Now I run the program by pressing F5 and let’s see the result

And now we can see that the result is the same for both methods, but now we can see that the new in .NET 7  method is a little bit cleaner syntax than the old ones.

We also have a option of sorting descending that is OrderDecending() to order the list in descending order for that we have to write 

var newSortedWeights = weights.OrderDescending();

Now I run the program by pressing F5 and let’s see the result for descending order also 

Next we are going to see ordering of the model lets say employee model which contains 2 properties name and age

internal class EmployeeModel

    {

        public string Name { get; set; }

        public int Age { get; set; }

    }

And now I want to order that 

So lets create demo data for that 

List<EmployeeModel> employee = new()

{

    new() { Name = "John", Age = 25 },

    new() { Name = "Ram", Age = 40 },

    new() { Name = "Ankit", Age = 20 },

};

So now you might think that you can order this model something like the previous example like 

var orderEmployee = employee.Order();

What made you think this worked or not? It is definitely not going to work because this is a complex object.

Let me run the code and show you the error 

The error indicates that something that implements IComparable is required because these two objects do not naturally compare to each other.

In this scenario, we have to write the old syntax of OrderBy  instead of Order()

And the new syntax will be

var orderedEmployee = employee.OrderBy(x => x.Name);

Lets write code to print the employee data on the screen

foreach (var e in orderEmployee)
{
    Console.WriteLine( $"{e.Name}-{e.Age}");
}

Now let’s run the application and see the output

Now we can see that employees are ordered by name.

So there is still a need of order by but if you have wither implemented

So Order() is the new way of doing things, but we still have OrderBy() when we have a more complex object that does not have an IComaparable on it.

So that’s the new Order and Order Descending operators on LINQ that allow us a quicker way to order things that haven’t implemented IComparable.

If you like this article also have a look @ .NET 7 Overview article where you can find the Changes and Support Cycle of it.

If you have Comment or Suggestion please let me know in the comment section or mail me @ [email protected]


You may also like...

1 Response

  1. December 2, 2022

    […] 2) Simplified LINQ Ordering […]

Leave a Reply

Your email address will not be published. Required fields are marked *