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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:02:20+00:00 2026-05-24T12:02:20+00:00

I would like to optimize following lines of code for Sorting. public ViewResult Index(string

  • 0

I would like to optimize following lines of code for Sorting.

public ViewResult Index(string sortorder, int? pagesize, int? page)
        {
            int pageSize = pagesize ?? 10;
            if (Request.HttpMethod != "GET")
            {
                page = 1;
                pageSize = 10;
            }
            ViewBag.SelectedPageSize = pageSize;

            ViewBag.CurrentSort = sortorder;
            ViewBag.FirstNameSortParm = String.IsNullOrEmpty(sortorder) ? "FirstName desc" : "";
            ViewBag.LastNameSortParm = sortorder == "LastName" ? "LastName desc" : "LastName";
            ViewBag.DepNameSortParm = sortorder == "depName" ? "depName desc" : "depName";

            var joined = from tm in db.TabMasters select tm;
            switch (sortorder)
            {
                case "FirstName":
                    joined = joined.OrderBy(m => m.FirstName);
                    break;
                case "FirstName desc":
                    joined = joined.OrderByDescending(m => m.FirstName);
                    break;
                case "LastName":
                    joined = joined.OrderBy(m => m.LastName);
                    break;
                case "LastName desc":
                    joined = joined.OrderByDescending(m => m.LastName);
                    break;
                case "depName":
                    joined = joined.OrderBy(m => m.depName);
                    break;
                case "depName desc":
                    joined = joined.OrderByDescending(m => m.depName);
                    break;
                default:
                    joined = joined.OrderBy(m => m.FirstName);
                    break;
            }

            int pageIndex = (page ?? 1) - 1;
            int start = (pageIndex * pageSize);
            ViewBag.TotalRecord = joined.Count();
            ViewBag.StartRecord = start + 1;
            ViewBag.EndRecord = ((start + pageSize) >= ViewBag.TotalRecord) ? ViewBag.TotalRecord : (start + pageSize);
            return View(joined.ToPagedList(pageIndex, pageSize));
        }

Because this is very tedious way if i have more the 10 fields to perform sort.

Thanks,
Imdadhusen

  • 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-24T12:02:21+00:00Added an answer on May 24, 2026 at 12:02 pm

    It’s a bit vague to me what your actual goal is but for the switch part you could use an extension method as the below.

    public static class SortExtensions
    {
        public static IEnumerable<T> SortByField<T>(this IEnumerable<T> sequence, string sortOrder)
        {
            var tokens = sortOrder.Trim().Split(' ');
            var field = tokens[0];
            var direction = tokens.Skip(1).Single().ToLower();
            var prop = typeof(T).GetProperty(field);
            return direction == "desc"
                       ? sequence.OrderByDescending(m => prop.GetValue(m, null))
                       : sequence.OrderBy(m => prop.GetValue(m, null));
        }
    }
    

    It will make a very simplified parsing of the sort order. It puts the responsibility on the calling party which is generally not what you want to do, so you might want some error handling in case the sortorder string does not fulfill the requirements.

    from the sortorder string it fetches a name used to identify a property which can be used to fetch the value used for sorting.

    you can use it like this:

    db.TabMasters.SortByField(sortOrder)
    

    EDIT based on comment:

    The line typeof(T).GetProperty(field) is fragile in the absence of any error handling. It relies on the first token to be a name of a public property of the type T. It will return null if the name doesn’t match a property. Including if it matches a Field name. A similar function exist for getting a FieldInfo
    prop.GetField(field) will return a fieldinfo object of there’s a public field with the given name otherwise null. To get the value of a field simply omit the last parameter to the GetValue call.

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

Sidebar

Related Questions

I came across the following code recently and would like to optimize it: Public
I would like to optimize the following method, that returns the total file count
I have a simple class like the following: class Record { public Record(string fp1,
I would like to use something like AutoMapper for the following code instead of
I would like to implement an B+ tree in Java and try to optimize
Would like to know the c# code to actually retrieve the IP type: Static
I would like to test a string containing a path to a file for
I would like to optimize my scp deployment which currently copies all files to
Let's say I have following code: int f() { int foo = 0; int
I would like some advice about the following Makefile. It works fine, but it

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.