| by Arround The Web | No comments

C# LINQ ThenBy() Method

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

ThenBy() Method in LINQ is used to return all elements in an ascending order within a given data structure based on multiple attributes. So we have to use the ThenBy() Method along with the OrderBy()/OrderByDescending() methods.

First we will apply the OrderBy()/OrderByDescending() method and ThenBy() is used.

Syntax:

If the values are single:

ThenBy(element => element)

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

If there are multiple values:

ThenBy(element => element.variable)

The element iterates the values in a list and arranges them in ascending order, and the variable is the value by which the values are arranged in ascending order based on this variable.

Overall Syntax:

list.OrderBy(element => element.variable).ThenBy(element => element.variable)..........

list.OrderByDescending(element => element.variable).ThenBy(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 ThenBy()

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 ThenBy().

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
       List first_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 ascending
       var ordered_data = first_list.OrderBy(element => element.food_price).ThenBy(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:

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 ThenBy) method to order the values in the name column in ascending 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 ascending order based on values in the name attribute.

Example 2: OrderByDescending() with ThenBy()

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 ThenBy().

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
       List first_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 ascending order.
var ordered_data = first_list.OrderByDescending(element => element.food_price).ThenBy(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 ThenBy() method to order the values in the name column in ascending 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 ascending order based on values in the name attribute.

Example 3: OrderBy() with multiple ThenBy()

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 ThenBy().

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
       List first_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 ascending order.
var ordered_data = first_list.OrderByDescending(element => element.food_price).
ThenBy(element => element.name).
ThenBy(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 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 the ThenBy() method to order the values in the name and quantity columns in ascending 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 ascending 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 ThenBy() method and 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