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
Newsletter
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#
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Trending

  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • Member Spotlight: Shai Almong
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see 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
·
Dec. 24, 11 · News
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
137.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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#
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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