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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:28:35+00:00 2026-05-28T15:28:35+00:00

I am writing a function with a signature like this where I am sorting

  • 0

I am writing a function with a signature like this where I am sorting the model data among other things:

public MyModel GetModel(IQueryable<Something> query, string sort, 
   int page, int PageSize)
{
   ...
   viewModel.Something = query.OrderByDescending(o => sort)
          .Skip((page - 1) * pageSize).Take(pageSize).ToList();
   ...
}

Problem is I want to pass in a default sort parameter which is what I want to sort against if the parameter “sort” is null or empty. For example, this might be:

.OrderByDescending(o => o.AddedDate);

I have tried all sorts of things (for instance passing in a Func) to pass in something to this function to tell it to use AddedDate or whatever I choose to sort the records if sort is null but nothing works. How would I implement such a thing?

  • 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-28T15:28:36+00:00Added an answer on May 28, 2026 at 3:28 pm

    Your current code doesn’t make much sense – you are sorting by a single value, not a property of the items you want to sort – if you want to be able to sort by any string property within your Something class more appropriate would be:

    public MyModel GetModel(IQueryable<Something> query, Func<Something, string> sort, 
       int page, int PageSize)
    {
       Func<Something, string> actualSort = sort ?? (o => o.AddedDate);
       ...
       viewModel.Something = query.OrderByDescending(actualSort)
              .Skip((page - 1) * pageSize).Take(pageSize).ToList();
       ...
    }
    

    Now if you pass in null for sort the sorting will revert to the default sort order by AddedDate.

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

Sidebar

Related Questions

I'm writing a function for an installer DLL to verify the Authenticode signature of
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I am writing a function short getBits(short data, int p, int n) I have
I am writing C extensions, and I'd like to make the signature of my
I'm passing around a partially applied function. The full signature is: import Data.Map as
I'm writing a Haskell library which uses Data.Vector 's. I successfully wrote library function,
I'm writing a function that fishes out the src from the first image tag
I am writing a function in Haskell that deals with numbers beyond the length
I'm writing a function that replaces long hex coded color ( #334455 ) with
Short of writing a function manually that translates a few known REFIID to names,

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.