| | 1 | | using Fluorite.Strainer.Models.Sorting; |
| | 2 | |
|
| | 3 | | namespace Fluorite.Extensions; |
| | 4 | |
|
| | 5 | | public static class SortExpressionQuerableExtensions |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Sorts the elements of a sequence in an order according to |
| | 9 | | /// <see cref="ISortExpression{TEntity}"/>. |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="TEntity"> |
| | 12 | | /// The type of entity stored in a sequence. |
| | 13 | | /// </typeparam> |
| | 14 | | /// <param name="source"> |
| | 15 | | /// The source sequence of elements. |
| | 16 | | /// </param> |
| | 17 | | /// <param name="sortExpression"> |
| | 18 | | /// The sort expression providing information about sorting order and key. |
| | 19 | | /// </param> |
| | 20 | | /// <returns> |
| | 21 | | /// An instance of ordered queryable sequence. |
| | 22 | | /// </returns> |
| | 23 | | /// <exception cref="ArgumentNullException"> |
| | 24 | | /// <paramref name="sortExpression"/> is <see langword="null"/>. |
| | 25 | | /// </exception> |
| | 26 | | /// <exception cref="ArgumentNullException"> |
| | 27 | | /// <paramref name="source"/> is <see langword="null"/>. |
| | 28 | | /// </exception> |
| | 29 | | public static IOrderedQueryable<TEntity> OrderWithSortExpression<TEntity>( |
| | 30 | | this IQueryable<TEntity> source, |
| | 31 | | ISortExpression<TEntity> sortExpression) |
| | 32 | | { |
| 9 | 33 | | Guard.Against.Null(source); |
| 8 | 34 | | Guard.Against.Null(sortExpression); |
| | 35 | |
|
| 7 | 36 | | if (sortExpression.IsSubsequent) |
| | 37 | | { |
| 2 | 38 | | var orderedSource = source as IOrderedQueryable<TEntity>; |
| 2 | 39 | | if (sortExpression.IsDescending) |
| | 40 | | { |
| 1 | 41 | | return orderedSource.ThenByDescending(sortExpression.Expression); |
| | 42 | | } |
| | 43 | | else |
| | 44 | | { |
| 1 | 45 | | return orderedSource.ThenBy(sortExpression.Expression); |
| | 46 | | } |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| 5 | 50 | | if (sortExpression.IsDescending) |
| | 51 | | { |
| 4 | 52 | | return source.OrderByDescending(sortExpression.Expression); |
| | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 1 | 56 | | return source.OrderBy(sortExpression.Expression); |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |
| | 60 | | } |