| by Arround The Web | No comments

C# LINQ ThenByDescending() Method

In this article, we will discuss how to order the data based on multiple attributes in descending order using the ThenByDescending() Method() 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 ThenByDescending() Method

ThenByDescending() Method in LINQ is used to return all elements in descending order within a given data structure based on multiple attributes. So we have to use the ThenByDescending() Method along with the OrderBy() method.

First we will apply the OrderBy()/OrderByDescending() method and it is followed by ThenByDescending().

Syntax:

If the values are single:

ThenByDescending(element => element)

The element iterates the values in a list and arranges them in descending order.

If there are multiple values:

ThenByDescending(element => element.variable)

The 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.

Overall Syntax:

list.OrderBy(element => element.variable).ThenByDescending(element => element.variable)….......

list.OrderByDescending(element => element.variable).ThenByDescending(element => element.variable)..........

Whereas, a list is the input list that holds values and a variable refers to an attribute name in which we will order based on this variable only.

Example 1: OrderBy() with ThenByDescending()

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

Order the values based on food_price with OrderBy() and food_name with ThenByDescending().

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=100,name="chips",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--------------------------");

         //order the data based on food_price values in ascending and name in descending
       var ordered_data = first_list.OrderBy(element =>element.food_price).ThenByDescending(element => element.name);  
            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:

So, we defined three attributes with food_price and quantity as an integer type and name as a 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 the food_price column in ascending order and ThenByDescending() method to order the values in the name column in descending order.

5. Display the result with a foreach loop.

So the entire list is ordered in ascending order based on values in food_price and in descending order based on values in the name attribute.

Example 2: OrderByDescending() with ThenByDescending()

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

Order the values based on food_price with OrderByDescending() and food_name with ThenByDescending().

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=100,name="chips",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--------------------------");

         //order the data based on food_price values in descending and name in descending order.
var ordered_data = first_list.OrderByDescending(element =>element.food_price).ThenByDescending(element => element.name);  
            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:

So we defined three attributes, with food_price and quantity as an integer type and name as a 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 OrderByDescending() to order the values based on the food_price column in descending order and ThenByDescending() method to order the values in the name column in descending order.

5. Display the result with a foreach loop.

So the entire list is ordered in descending order based on values in food_price and in descending order based on values in the name attribute.

Example 3: OrderBy() with multiple ThenByDescending()

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

Order the values based on food_price with OrderByDescending() and food_name,quantity with ThenByDescending().

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=100,name="chips",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--------------------------");

         //order the data based on food_price values in descending and name,quantity in descending order.
var ordered_data = first_list.OrderByDescending(element =>element.food_price).
ThenByDescending(element => element.name).
ThenByDescending(element =>element.quantity);
            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:

So we defined three attributes with food_price and quantity as an 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 OrderByDescending() to order the values based on the food_price column in descending order and ThenByDescending() method to order the values in the name and quantity columns in descending order.

5. Display the result with a foreach loop.

So the entire list is ordered in descending order based on values in food_price and in descending order based on values in name and quantity attributes.

Conclusion

In the LINQ tutorial, we saw how to order the data by multiple attributes with the ThenByDescending() method, along with the OrderBy()/OrderByDescending() functions. It is also possible to order the data based on a particular value by providing multiple attributes. We discussed three different examples to understand the concept better. Make sure that you import the using System, System.Linq and System.Collections.Generic;

Share Button

Source: linuxhint.com

Leave a Reply