DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Features of C# 9 That Will Make Your Life Easier [Snippets]
  • Creating a Business Rule Engine Using Dynamic Expression Predicates With C#
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines

Trending

  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Optimizing Databricks Spark Pipelines Using Declarative Patterns
  • Design Patterns for GenAI Creative Systems in Advertising
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to order by multiple columns using Lambas and LINQ in C#

How to order by multiple columns using Lambas and LINQ in C#

By 
Senthil Kumar user avatar
Senthil Kumar
·
Dec. 24, 11 · News
Likes (0)
Comment
Save
Tweet
Share
136.8K Views

Join the DZone community and get the full member experience.

Join For Free

Today, I was required to order a list of records based on a Name and then ID. A simple one, but I did spend some time on how to do it with Lambda Expression in C#. C# provides the OrderBy, OrderByDescending, ThenBy, ThenByDescending. You can use them in your lambda expression to order the records as per your requirement.

Assuming your list is “Phones” and contains the following data

public class Phone
{
       public int ID { get; set; }
       public string Name { get; set; }
}

public class Phones : List
{
        public Phones()
        {
            Add(new Phone { ID = 1, Name = "Windows Phone 7" });
            Add(new Phone { ID = 5, Name = "iPhone" });
            Add(new Phone { ID = 2, Name = "Windows Phone 7" });
            Add(new Phone { ID = 3, Name = "Windows Mobile 6.1" });
            Add(new Phone { ID = 6, Name = "Android" });
            Add(new Phone { ID = 10, Name = "BlackBerry" });
        }
}

If you were to use LINQ Query , the query will look like the one below

dataGridView1.DataSource = (from m in new Phones()
                                       orderby m.Name, m.ID
                                       select m).ToList();

Simple isn’t it ? It very simple using Lamba expression too. Your Lambda’s expression for the above LINQ query will look like the one below

dataGridView1.DataSource = new Phones().OrderByDescending(a => a.Name).ThenByDescending(a => a.ID).ToList();

Database csharp

Published at DZone with permission of Senthil Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Features of C# 9 That Will Make Your Life Easier [Snippets]
  • Creating a Business Rule Engine Using Dynamic Expression Predicates With C#
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook