Benny Sutton is 4 hire - just click here

Extension Method for LINQ conditional expressions

A general solution, that can be used for various conditional lambda expressions without breaking the flow of the expression.

           
        public static IEnumerable<T> IfThenElse<T>(
        this IEnumerable<T> elements,
        Func<bool> condition,
        Func<IEnumerable<T>, IEnumerable<T>> thenPath,
        Func<IEnumerable<T>, IEnumerable<T>> elsePath)
        {
            return condition()
                ? thenPath(elements)
                : elsePath(elements);
        }
        ///var result = widgets
        ///    .Where(w => w.Name.Contains("xyz"))
        ///    .IfThenElse(
        ///() => flag,
        /// e => e.OrderBy(w => w.Id),
        /// e => e.OrderByDescending(w => w.Id));