LINQ (C#) – ThenBy and ThenByDescending Tutorial

In this tutorial, you will learn:
  1. What are ThenBy and ThenByDescending Operator in LINQ?
  2. How to sort using ThenBy and ThenByDescending in LINQ C#?
  3. Programming Example

ThenBy and ThenByDescending, both are secondary sorting operator in LINQ C# and it is used after OrderBy. OrderBy and OrderByDescending is the primary sorting operator. ThenBy basically used when you need to sort another column along with primary column. You can understand it better by observing following programming example.

ThenBy Programming Example

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

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<ProductStore> productList = new List<ProductStore>();

            productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 2000 });
            productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
            productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
            productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
            productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 });
                        
            var result = productList.OrderBy(p => p.productName).ThenBy(p => p.productPrice);

            foreach (var list in result)
            {
                Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice);
            }

            Console.ReadKey();
        }
    }
}


Output:


Product Name: Bluetooth | Product Price : 540
Product Name: Hard Disk | Product Price : 1280
Product Name: Keyboard & Mouse | Product Price : 1130
Product Name: Monitor | Product Price : 2000
Product Name: Monitor | Product Price : 3000
Product Name: Monitor | Product Price : 3500
Product Name: Processor | Product Price : 7680
Product Name: RAM | Product Price : 2450
Product Name: SSD Disk | Product Price : 3500
_


Explanation

In the above example, focus on marked line on list. You will notice that there are 3 products with the same name of Monitor but the prices are different. When we sort productPrice column using thenby operator, you can see that monitor are sorted according to price.


ThenByDescending Programming Example

ThenByDescending is used when you need to sort secondary column in descending order.

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

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<ProductStore> productList = new List<ProductStore>();

            productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "Monitor", productPrice = 2000 });
            productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 });
            productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 });
            productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 });
            productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 });
            productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 });
                        
            var result = productList.OrderBy(p => p.productName).ThenByDescending(p => p.productPrice);

            foreach (var list in result)
            {
                Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice);
            }

            Console.ReadKey();
        }
    }
}

Output

Product Name: Bluetooth | Product Price : 540
Product Name: Hard Disk | Product Price : 1280
Product Name: Keyboard & Mouse | Product Price : 1130
Product Name: Monitor | Product Price : 3500
Product Name: Monitor | Product Price : 3000
Product Name: Monitor | Product Price : 2000
Product Name: Processor | Product Price : 7680
Product Name: RAM | Product Price : 2450
Product Name: SSD Disk | Product Price : 3500


_

In this example, you can see that Monitor are sorted in descending order based on their productPrice.

Summary

In this tutorial, I have simply tried to teach you ThenBy and ThenByDescending LINQ operator using simple c# program. You can use both ThenBy and ThenByDescending operator to sort additional column. In the next chapter, you will learn Reverse Operator in LINQ.

 

Share your thought