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

  • SEARCH
  • Home
  • 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 8892041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:53:43+00:00 2026-06-14T22:53:43+00:00

I have a reporting interface where the end user gets to select multiple fields

  • 0

I have a reporting interface where the end user gets to select multiple fields for the sort order of the returned report. The problem I am having is that I can’t really chain the OrderBy / ThenBy methods, since I’m iterating through a list of sort fields. I’m thinking something like this:

foreach (string sort in data.SortParams)
{
    switch (sort)
    {
        case "state":
            query = query.ThenBy(l => l.RegionCode);
            break;
        case "type":
            query = query.ThenBy(l => l.Type);
            break;
        case "color":
            query = query.ThenBy(l => l.Color);
            break;
        case "category":
            query = query.OrderBy(l => l.Category);
            break;
    }
}

(Note: I’ve removed the switch determining if this is the first sort item for simplicity’s sake.)

Any thoughts on how to iterate through a collection to determine the sort order?

  • 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-06-14T22:53:44+00:00Added an answer on June 14, 2026 at 10:53 pm

    You could do what you want if you use an initial “seed” OrderBy:

    EDIT you need to call OrderBy to create an IOrderedEnumerable (or IOrderedQueryable) first before attaching ThenBy clauses:

    var orderedQuery = query.OrderBy(l => 0);
    foreach (string sort in data.SortParams)
    {
        switch (sort)
        {
            case "state":
                orderedQuery = orderedQuery.ThenBy(l => l.RegionCode);
                break;
            case "type":
                orderedQuery = orderedQuery.ThenBy(l => l.Type);
                break;
            case "color":
                orderedQuery = orderedQuery.ThenBy(l => l.Color);
                break;
            case "category":
                orderedQuery = orderedQuery.ThenBy(l => l.Category);
                break;
        }
    }
    query = orderedQuery;  // cast back to original type.
    

    If you want something more flexible check out this answer

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

Sidebar

Related Questions

We have a reporting solution that works like this. User orders the report to
I have a multipage reporting services report, and i would like to start from
i have strange problem doing reporting: i have numerous clients with different issued invoices.
I have an SQL Server Reporting Services (SSRS) report, that uses a view. That
I have a SQL Server Reporting Services report with an embedded image in the
I have a Reporting Services 2005 report that had an error: Parameter UserID is
I have a report showed as bar chart in sql server reporting services. when
I have a reporting requirement in my web application for which I am looking
I have a large reporting table in Excel that looks similar to the following.
We have an automatic reporting and notification system written in .net that sends emails

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.