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

  • Home
  • SEARCH
  • 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 myself writing code that looks like this a lot: set<int> affected_items; while
I often end up writing code like if x == 1 or x ==
I realized that I'm writing a lot of code similar to this one: <%
I often end up writing a bit of code twice when using a loops.
I am writing front end app that connects to a DB and downloads any
I'm writing a Cocoa OS X (Leopard 10.5+) end-user program that's using timestamps to
i am writing this code: awk -F'=' '!/^$/{arr[$1]=$2}END{for (x in arr) {print x=arr[x]}}' 1.txt
Background : I'm writing an application in C# (.NET 3.5), that looks at multiple
All right, I'm at my wit's end. I'm writing a droid app that receives
I am writing a lot of JQuery code in a page and have decided

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.