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 1105065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:34:23+00:00 2026-05-17T01:34:23+00:00

I basically have a table with headers that I read in from a DB

  • 0

I basically have a table with headers that I read in from a DB using linq in C#. Of these headers, there is always at least one that is always the same; Total and I want it to always be on the right.

So here is sort of how my data is laid out:

Data
{
   Label,
   Value,
   Age  //Not used initially
}

Sample Data:

{"Dog", 7}
{"Cat", 3}
{"Other", 4}
{"Total", 14}

I’d like to order the labels in this order; the actual animal names are sorted by their value in descending order and Total is appended to the end:

"Dog", "Other", "Cat", "Total"

How do I do this in Linq. How do I order an attribute based upon the value of another attribute?

Once I have the order of the headers, is there an easy way to order future rows based upon the already determined order. If I want to initially find headers where(x=>x.Age > 20)
how can I sort the Labels in where(x=>x.Age <= 20) based upon the same ordering as the >20 set?

  • 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-17T01:34:24+00:00Added an answer on May 17, 2026 at 1:34 am

    This query should work:

    var query = from row in table
                let ignore = row.Label == "Total"
                orderby ignore, row.Value descending
                select row.Label;
    
    //and corresponding lambda version
    var lquery = table.OrderBy(row => row.Label == "Total")
                      .ThenByDescending(row => row.Value)
                      .Select(row => row.Label);
    

    Note: it isn’t entirely necessary to create a ignore variable, it could be placed directly in the orderby clause. This way, it makes it more readable.

    Not sure what you are asking in your second question.


    edit:
    In response to your comment, it would depend on how you wanted the sorting to work, at least written like this. This works well if there is only one row in the table that you want to completely ignore. Not so much if you had more than one. The problem being that among the “ignored” rows, will be sorted by the original sorting as well (in this case, by Value). A naive way to add another row is to add to the ignore condition.

    var query = from row in table
                let ignore = row.Label == "Total" || row.Label == "Cost"
                orderby ignore, row.Value descending
                select row.Label;
    

    To have a specific ordering among the “ignored” rows, it would require a somewhat more complex query:

    var query = from row in table
                let ignore = row.Label == "Total" || row.Label == "Cost"
                let ignoreorder = row.Label == "Cost" ? 1 : 0
                orderby ignore, ignoreorder, row.Value descending
                select row.Label;
    
    //and corresponding lambda version
    var lquery = table.OrderBy(row => row.Label == "Total" || row.Label == "Cost")
                      .ThenBy(row => row.Label == "Cost" ? 1 : 0)
                      .ThenByDescending(row => row.Value)
                      .Select(row => row.Label);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing basically the same issue described here I have a table that starts
So basically I have a table - ID | from | To ----------------- 1
If I were to select a row from a table I basically have two
Basically I have a database with two tables, that is, Updates table and Images
Is there any name for the following DB table design: Basically we have generic
I'm creating an output in Java using printf() to create table headers. One of
just wondering is there any way that I can return one value from my
I have a table that contains rows with different class names. Some of these
This one is weird, I have a page that consists of a html table
I have a view that displays a table: rows represent days - one row

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.