c sharp

LINQ OrderBy() Method

In this article, we will discuss how to order the data using the OrderBy() 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 OrderBy()

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

OrderBy() 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.OrderBy(element => element)

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

If there are multiple values:

list.OrderBy(element => element.variable)

element iterates the values in a list and arranges them in ascending order. Variable is the value in which the values are arranged in ascending 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 OrderBy() 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
       List<int> first_list = new List<int>() {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 the list
       var ordered_data = first_list.OrderBy(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. So first, we created a list data structure with 10 integers.

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 2:

Here, we will create a list that holds string elements and we will use OrderBy() 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
       List<string> first_list = new List<string>() {"Linuxhint","sravan","kumar","A"};
       
       
       Console.WriteLine("List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
           
           
        //order the elements in the list
       var ordered_data = first_list.OrderBy(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. So 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./li>

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
       List<Food> first_list = new List<Food>();
       //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----------------------");

         //order the data based on food_price values
       var ordered_data = first_list.OrderBy(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:

So, 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.

So, the entire list is ordered in ascending order based on values in food_price.

Conclusion

In this C# – LINQ tutorial, we saw how to order the data with the OrderBy() function. It will return all elements in an 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.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain