public static IOrderedQueryable <T> AddOrderBy <T, TKey>(this IOrderedQueryable <T> q, Expression <Func <T, TKey> > orderExpression, bool isDescending = false) { var hasBeenOrdered = q.IsOrdered(); if (hasBeenOrdered) { return(!isDescending?q.ThenBy(orderExpression) : q.ThenByDescending(orderExpression)); } return(!isDescending?q.OrderBy(orderExpression) : q.OrderByDescending(orderExpression)); }