Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6228225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:21:07+00:00 2026-05-24T09:21:07+00:00

I am querying a table of Order elements with LINQ (C#). Each Order has

  • 0

I am querying a table of Order elements with LINQ (C#). Each Order has the following fields:

- ID
- OpenDate
- PriorityID
- StatusID
- Description

The StatusID field maps to a Status table. The Status table is structured as:

- ID
- Name

I need to get all of the Order objects sorted by their Priority and Status.
I can successfully get the Order objects sorted by Priority. I’m doing this via the
following:

List<Order> orders = new List<Order>();
using (DBDataContext context = new DBDataContext())
{
  orders = (from o in context.Orders
            orderby (o.PriorityID.HasValue ? o.PriorityID : Int32.MaxValue)  ascending
            select o).ToList();
}

But my problem is factoring in the Status.

Once the order objects have been sorted by priority, I need to sort the Order objects
in the following order of Status: Cancelled, Open, In-Route, and Delivered. Significantly,
The IDs of these Status values are firmly set in a random, non-helpful order. I cannot alter them. As you can tell, I can’t sort the status by alphbetical name either. In addition, I can’t add any fields to my database. Can anyone tell me how I can solve this problem in LINQ?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:21:08+00:00Added an answer on May 24, 2026 at 9:21 am

    I think you have solution to this by implement IComparer and use Linq to order it. Since your Status is not in numeric nor alphabet order.

    public class CustomComparer : IComparer<Status>
    {
         public int Compare(Status statusA, Status statusB)
         {
           if (statusA.StatusName == "Cancelled" && statusB.StatusName == "Cancelled")
           {
              return 0; // equals
           } 
           else if (statusA.StatusName == "Cancelled" && statusB.StatusName != "Cancelled")
           {
              return 1; // A > B
           }
           ....
         }
    }
    

    then

    orders.OrderBy(x => x.Status, new CustomComparer())
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a list of businesses. Each row has details such
I have two tables that I'm querying from. In one table, there are fields
Querying a Nested Set Model table , here's the SQL... how can this be
Well I am querying my DB, a table called bookBilling, to get a value
I have some data I am querying. The table is composed of two columns
I need to order rows over their column source . However, here's the table
I'm querying a bunch of cities from a zip-code table. I'd only like to
I have the following table: create table stuff ( id mediumint unsigned not null
I'm querying some data from a table in DB2/z which holds hourly data but
I have some php querying and printing the info of a mysql table on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.