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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:12:48+00:00 2026-06-13T16:12:48+00:00

Is it possible to choose which function to apply in Linq? Example of what

  • 0

Is it possible to choose which function to apply in Linq?
Example of what I now have:

List<int> list = new List<int>();
bool bDescend = false;
var listOrdered = bDescend ? list.OrderByDescending(it => it) : 
                             list.OrderBy(it => it);

You see that I choose which function to apply depending on bDescend value – so there is actually two Linq queries, not one.
Is it possible to avoid it by putting condition inside one query?
Maybe something like

list.ChooseFunction(bDescend ? OrderBy : OrderByDescending)...

Real code is more compliated and if such trick possible it would make code much more readable.
Looks like kind of metaprogramming..

  • 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-06-13T16:12:49+00:00Added an answer on June 13, 2026 at 4:12 pm

    You could create your own Extension method:

    enum Ordering
    {
        Ascending,
        Descending,
    }
    
    // trivial to convert to use booleans instead
    // of an enum if that's truly what you need
    public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
        this IEnumerable<TSource> source, Ordering ordering, Func<TSource, TKey> keySelector) 
    {
        if (ordering == Ordering.Ascending)
        {
            return source.OrderBy(keySelector);
        }
        else
        {
            return source.OrderByDescending(keySelector);
        }
    }
    

    And then call it like

    var order = Ordering.Descending;
    ....
    MyList.OrderBy(order, it => it);
    

    Other than that, it’s possible to make a ChooseFunction type thing, but it’d have to look like ChooseFunction(bdescend ? "OrderBy" : "OrderByDescending") and I don’t even want to get into how ugly those magic strings referring to function names are. You’d also have to either hard code the ChooseFunction extension, or rely on reflection which is ridiculously expensive for the simple thing you’re trying to do

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

Sidebar

Related Questions

From what I understand, I have to choose between calloc , which will allocate
Possible Duplicate: Choose order to execute JUnit tests I am writing a JUnit test.
Is it possible to choose the whisker value with proc sgplot. Because it seems
I want to know if its possible to choose a GC algorithm via code
When using <input type=file multiple> it's possible for the user to choose multiple files.
Possible Duplicate: How to save in the dropdown the value choosed? I have a
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
I'm sorting array: myArray.sort(comparators.some_comparator); and I have several comparator to choose from: comparators =
I have the following drop down: <select name=chooseLanguage> <option>Choose One</option> <option value=java>Java</option> <option value=php>PHP</option>
That may sound a little confusing. Basically, I have a function CCard newCard() {

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.