static IOrderedQueryable <T> OrderByRulesRecursivly <T>(IOrderedQueryable <T> dataCollection, List <OrderRule> rules) { if (!rules.Any()) { return(dataCollection); } // apply first rule OrderRule rule = rules.First(); MethodInfo orderOperator = rule.Descending ? ThenByDescendingMethodInfo : ThenByMethodInfo; IOrderedQueryable <T> orderedDataCollection = OrderByFieldOrPropertyName(dataCollection, orderOperator, rule.FieldOrPropertyName); // apply next rules recursivly return(OrderByRulesRecursivly(orderedDataCollection, rules.Skip(1).ToList())); }
/// <summary> /// Order <paramref name="dataCollection"/> according to <paramref name="rules"/> sequence /// </summary> /// <typeparam name="T">Collection item type</typeparam> /// <param name="dataCollection">Queryable collection</param> /// <param name="rules">Order rules to apply</param> /// <returns>Ordered queryable collection</returns> public static IOrderedQueryable <T> OrderByRules <T>(this IQueryable <T> dataCollection, IEnumerable <OrderRule> rules) { if (!rules.Any()) { throw new ArgumentException("Rules list is empty", "rules"); } // apply first rule (special case: use OrderBy operator and not ThenBy) OrderRule rule = rules.First(); MethodInfo orderOperator = rule.Descending ? OrderByDescendingMethodInfo : OrderByMethodInfo; IOrderedQueryable <T> orderedDataCollection = OrderByFieldOrPropertyName(dataCollection, orderOperator, rule.FieldOrPropertyName); // apply next rules recursivly return(OrderByRulesRecursivly(orderedDataCollection, rules.Skip(1).ToList())); }