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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:40:34+00:00 2026-05-30T23:40:34+00:00

I had a typed DataTable which to sort was just something like: DataTable.DefaultView.Sort(sortexpression); Because

  • 0

I had a typed DataTable which to sort was just something like:

DataTable.DefaultView.Sort("sortexpression");

Because the sort expression was a string I could just append both the sort field and direction in a couple of lines, regardless of how many sort options I had. Now with Linq Im clearly doing something very wrong because to do something similar I’m doing this:

             this.GetSortExpressions();
        if ((ViewState["SortDirection"] as string) == "ASC")
        {
            switch (ViewState["SortField"] as string)
            {
                case "LKey":
                    this.SortedDetails = this.Details.OrderBy(d => d.LKey);
                    break;
                case "MName":
                    this.SortedDetails = this.Details.OrderBy(d => d.MaterialName);
                    break;
                case "FMSQOH":
                    this.SortedDetails = this.Details.OrderBy(d => d.FMSQOH);
                    break;
                case "CCQOH":
                    this.SortedDetails = this.Details.OrderBy(d => d.CCQOH);
                    break;
                case "FMSQOHVary":
                    this.SortedDetails = this.Details.OrderBy(d => d.FMSQOHVary);
                    break;
                default:
                    this.SortedDetails = this.Details.OrderBy(d => d.LKey);
                    break;
            }
        }
        else
        {
            switch (ViewState["SortField"] as string)
            {
                case "LKey":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.LKey);
                    break;
                case "MName":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.MaterialName);
                    break;
                case "FMSQOH":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.FMSQOH);
                    break;
                case "CCQOH":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.CCQOH);
                    break;
                case "FMSQOHVary":
                    this.SortedDetails = this.Details.OrderByDescending(d => d.FMSQOHVary);
                    break;
                default:
                    this.SortedDetails = this.Details.OrderByDescending(d => d.LKey);
                    break;
            }
        }

It’s god awful. I’m concerned about the fact that Im adding 2*n case statements for every new sort field. What is the right way please?

  • 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-30T23:40:35+00:00Added an answer on May 30, 2026 at 11:40 pm

    I have been gone through this.. what i found lastly was a generic solution that use reflection to sort a IEnumerable object.

    http://zhousanfeng.wordpress.com/2009/12/01/a-generic-comparersorter-class%E8%BD%AC/

    this will be how you could use it

    Sorter<TestClass> sort = new Sorter<TestClass>(this.Deatils, "LKey ASC");
    List<TestClass> sorted = sort.GetSortedList();
    

    Regards.

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

Sidebar

Related Questions

When I had a Typed DataTable with information retrieved from a SQL Server with
How do I call an external command within Python as if I had typed
I had thought I could enumerate the types using IMetaDataImport.EnumTypeDefs and for each of
Using Script Task, I had stored data ie a Dataset or Datatable object to
So c# had evolved as staticly typed language and does a more than a
Has anybody had a chance to compare and measure performance of LINQ with typed
Given the strong type system of Scala, I had an ambitious project which I'm
I have a DataTable that's had people wire into it's different column changed events,
I'm trying to perform a simple Linq find first query on a typed DataTable,
I was wondering if anyone here had some experience writing this type of script

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.