You can create your own extension method:
enum Ordering { Ascending, Descending, } // trivial to convert to use booleans instead // of an enum if that 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);
Itβs also possible to create an element of type ChooseFunction , but it should look like ChooseFunction(bdescend ? "OrderBy" : "OrderByDescending") , and I donβt even want to understand how ugly those magic lines that refer to function names are ugly. You will also have to either hardcode the SelectFunction extension or rely on reflection, which is ridiculously expensive for the simple thing you're trying to do
source share