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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:17:07+00:00 2026-05-12T13:17:07+00:00

I end up writing a lot of code that looks like var ordered =

  • 0

I end up writing a lot of code that looks like

var ordered = isDescending ? queryable.OrderByDescending(x => x.ID) : queryable.OrderBy(x => x.ID)

but with different expressions such as x => x.DateOfBirth etc. What I would like to do is place that in a generic extension method that I could parse my expression, and a boolean isDescending to, but I’m not sure how to do this. Something like

public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, Expression<Func<T, TKey>> func, bool isDescending) {
  return isDescending ? source.OrderByDescending(func) : source.OrderBy(func);
}

Can anyone tell me how to do this? Preferably with an explanation so that I can understand it.

  • 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-12T13:17:07+00:00Added an answer on May 12, 2026 at 1:17 pm

    You’re very nearly there – you’re just missing a type parameter:

    public static IOrderedQueryable<T> OrderBy<T, TKey>(
      this IQueryable<T> source, Expression<Func<T, TKey>> func, bool isDescending) {
      return isDescending ? source.OrderByDescending(func) : source.OrderBy(func);
    }
    

    Your code wasn’t working before because the func parameter’s type used TKey, and that wasn’t one of the type parameters in your method declaration. The above should work fine.

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

Sidebar

Related Questions

I find that I frequently end up writing a function that I always call
I'm writing some cross-platform code between Windows and Mac. If list::end() "returns an iterator
I'm writting a simple prototype front end using a GridView that is populated via
The code at the end produces a compile error: NotApplicable.java:7: run() in cannot be
I'm developing front-end code for a web application, and ran into an odd piece
On the other end of the spectrum, I would be happy if I could
All front-end developers know the pain of coding for Firefox, then viewing our then
I'm a front-end developer and I was looking for opinions about the best all-round
A poorly-written back-end system we interface with is having trouble with handling the load
When hiring a front-end developer, what specific skills and practices should you test for?

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.