| by Arround The Web | No comments

LINQ OrderByDescending() Method

In this article, we will discuss how to order the data in descending order using the OrderByDescending() method through LINQ.

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures. It is used to perform queries similar to SQL like expressions.

LINQ OrderByDescending()

OrderByDescending() in LINQ is used to return all elements in a descending order within a given data structure.

OrderByDescending() is also applied on the data that has multiple values in each row. It is possible to order the data based on a particular value in each row.

Syntax:

If the values are single:

list.OrderByDescending(element => element)

element iterates the values in a list and arrange them in Descending order.

If there are multiple values:

list.OrderByDescending(element => element.variable)

element iterates the values in a list and arranges them in descending order and the variable is the value in which the values are arranged in Descending order based on this variable.

Let’s explore this method.

Example 1:

Here, we will create a list that holds integer elements and we will use OrderByDescending() to return these elements in an order.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create data
       Listfirst_list = new List() {120,80,45,123,456,45,120,8,9,0 };
   
   
Console.WriteLine("List: ");
    foreach (var values in first_list)
    {
Console.WriteLine(values);
    }
   
   
    //order the elements in descending order
    var ordered_data = first_list.OrderByDescending(element => element);
   
Console.WriteLine("Ordered data: ");
   
    //return one by one value from the ordered_data
    foreach (var result in ordered_data)
    {
Console.WriteLine(result);
    }
   
}
}

Output:

Explanation:

1. We created a list data structure with 10 integers.

2. After that, we applied OrderByDescending() to order that list by iterating the list using iterator-element.

3. Finally, we can display the result by iterating the ordered_data using a foreach loop.

Example 2:

Here, we will create a list that holds string elements and we will use OrderByDescending() to return these elements in descending order.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create data
       Listfirst_list = new List() {"Linuxhint","sravan","kumar","A"};
   
   
    Console.WriteLine("List: ");
    foreach (var values in first_list)
    {
Console.WriteLine(values);
    }
   
   
    //order the elements in descending order
    var ordered_data = first_list.OrderByDescending(element => element);
   
Console.WriteLine("Ordered data: ");
   
    //return one by one value from the ordered_data
    foreach (var result in ordered_data)
    {
    Console.WriteLine(result);
    }
   
}
}

Output:

Explanation:

1. First, we created a list data structure with 4 strings.

2. After that, we applied OrderBy() to order that list by iterating the list using iterator-element.

3. Finally, we can display the result by iterating the ordered_data using a foreach loop.

Example 3:

Let’s create Food that holds three attributes – food_price, name, and quantity.

Order the values based on food_price.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
    //define the data for Food
   class Food
    {
    public int food_price { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    }
    static public  void Main(){
   
    //create data
       Listfirst_list = new List();
    //add values
first_list.Add(new Food { food_price=300,name="parota",quantity=1 });
first_list.Add(new Food { food_price=800,name="paneer",quantity=4 });
first_list.Add(new Food { food_price=100,name="mushroom",quantity=2 });
first_list.Add(new Food { food_price=564,name="vegtables",quantity=10 });
    first_list.Add(new Food { food_price=400,name="fruits",quantity=8 });
   
    foreach (var value in first_list)
    {
                Console.WriteLine(value.food_price+"->"+value.name+"->"+value.quantity);
    }
   
   
   
Console.WriteLine("--------------------------Ordered data (Descending)--------------------------");

    //order the data based on food_price values in Descending order
    var ordered_data = first_list.OrderByDescending(element =>element.food_price);
            foreach (var result in ordered_data)
    {
                Console.WriteLine(result.food_price+"->"+result.name+"->"+result.quantity);
    }
   
}
}

Output:

Explanation:

1. First, we have to declare the structure:

We defined three attributes with food_price and quantity as integer type and name as string type.

2. Next, we created a list named first_list from the structure-Food.

3. Add the values to the above created list.

We have added 5 values.

4. Apply OrderBy() to order the values based on food_price column.

5. Display the result with a foreach loop.

The entire list is ordered in descending order based on values in food_price.

Conclusion

In this C# – LINQ tutorial, we saw how to order the data in descending order with the OrderByDescending() function. It will return all elements in descending order within a given data structure. It is also possible to order the data based on a particular value in each row. We discussed three different examples to understand the concept better.

Share Button

Source: linuxhint.com

Leave a Reply