using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Documents.Queries; using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using zero.Core.Entities; namespace zero.Core.Extensions { public static class QueryableExtensions { public static IOrderedQueryable OrderBy(this IQueryable source, string path, bool isDescending, OrderingType type = OrderingType.String) { if (!String.IsNullOrEmpty(path)) { char[] a = path.ToCharArray(); a[0] = char.ToUpper(a[0]); path = new string(a); } if (isDescending) { return source.OrderByDescending(path, type); } return source.OrderBy(path, type); } public static IOrderedQueryable ThenBy(this IOrderedQueryable source, string path, bool isDescending, OrderingType type = OrderingType.String) { if (!String.IsNullOrEmpty(path)) { char[] a = path.ToCharArray(); a[0] = char.ToUpper(a[0]); path = new string(a); } if (isDescending) { return source.ThenByDescending(path, type); } return source.ThenBy(path, type); } public static IQueryable Paging(this IQueryable source, int pageNumber, int pageSize) { if (pageNumber <= 0 || pageSize <= 0) { throw new NotSupportedException("Both pageNumber and pageSize must be greater than zero"); } return source.Skip((pageNumber - 1) * pageSize).Take(pageSize); } public static IQueryable WhereIf(this IQueryable source, Expression> predicate, bool condition, Expression> elsePredicate = null) { if (!condition) { if (elsePredicate != null) { return source.Where(elsePredicate); } return source; } return source.Where(predicate); } public static IQueryable SearchIf(this IQueryable source, Expression> fieldSelector, string searchTerms, string suffix = null, string prefix = null, SearchOperator @operator = SearchOperator.Or) { string search; if (String.IsNullOrWhiteSpace(searchTerms)) { return source; } search = searchTerms; if (suffix != null) { search += suffix; } if (prefix != null) { search = prefix + search; } return source.Search(fieldSelector, search.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries), @operator: @operator); } } }