/// <summary> /// Order an <see cref="IEnumerable{TSource}"/> /// </summary> /// <typeparam name="TSource"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="source"></param> /// <param name="orderitem"><see cref="IOrderItem"/></param> /// <returns></returns> public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey>(this IEnumerable <TSource> source, OrderItem <TKey> orderitem) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrWhiteSpace(orderitem.SortColum)) { throw new ArgumentException("Input cannot be null or empty", "orderitem.SortColum"); } return(OrderByInternal.EnumerableOrder(source, orderitem, out _)); }
/// <summary> /// Order an <see cref="IEnumerable{TSource}"/> /// </summary> /// <typeparam name="TSource"></typeparam> /// <param name="source"></param> /// <param name="sortColum">name of the column to sort on</param> /// <param name="isDescending">Is the order direction descending</param> /// <returns></returns> public static IOrderedEnumerable <TSource> OrderBy <TSource>(this IEnumerable <TSource> source, string sortColum, bool isDescending) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrWhiteSpace(sortColum)) { throw new ArgumentException("Input cannot be null or empty", "sortColum"); } return(OrderByInternal.EnumerableOrder(source, new OrderItem { SortColum = sortColum, OrderDirection = Orderby.GetOrderDirection(isDescending) }, out _)); }
/// <summary> /// Order an <see cref="IEnumerable{TSource}"/> /// </summary> /// <typeparam name="TSource"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="source"></param> /// <param name="sortColum">Comma or semicolumn seperated string expression of the colume and direction. Directon is not mandatory, if not pressend then it will be ascending</param> /// <param name="orderDirection"><see cref="OrderDirectionEnum"/></param> /// <param name="comparer"><see cref="IComparer{TKey}" /></param> /// <returns></returns> public static IOrderedEnumerable <TSource> OrderBy <TSource, TKey>(this IEnumerable <TSource> source, string sortColum, OrderDirectionEnum orderDirection, IComparer <TKey> comparer) { if (source == null) { throw new ArgumentNullException("source"); } if (string.IsNullOrWhiteSpace(sortColum)) { throw new ArgumentException("Input cannot be null or empty", "sortColum"); } return(OrderByInternal.EnumerableOrder(source, new OrderItem <TKey> { SortColum = sortColum, OrderDirection = orderDirection, Comparer = comparer }, out _)); }
/// <summary> /// Order an <see cref="IEnumerable{TSource}"/> /// </summary> /// <typeparam name="TSource"></typeparam> /// <param name="source"></param> /// <param name="orderitems"><see cref="IEnumerable{IOrderItem}"/></param> /// <returns></returns> public static IOrderedEnumerable <TSource> OrderBy <TSource>(this IEnumerable <TSource> source, IEnumerable <IOrderItem> orderitems) { if (source == null) { throw new ArgumentNullException("source"); } if (!orderitems.Any()) { throw new ArgumentNullException("orderitems"); } if (orderitems.Any(c => string.IsNullOrWhiteSpace(c.SortColum))) { throw new ArgumentException("Input cannot be null or empty", "orderitems.SortColum"); } var isOrderby = true; var processed = false; //Can not convert source to IOrderedQueryable // => gives error IOrderedEnumerable <TSource> returnOrder = null; foreach (var orderitem in orderitems) { returnOrder = isOrderby ? OrderByInternal.EnumerableOrder(source, orderitem, out processed) : OrderByInternal.EnumerableOrderThenBy(returnOrder, orderitem); if (isOrderby && processed) { isOrderby = false; } } return(returnOrder); }