using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Documents.Queries; using Raven.Client.Documents.Session; using System; using System.Linq; using System.Linq.Expressions; 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) { pageNumber = pageNumber.Limit(1, 10_000_000); pageSize = pageSize.Limit(1, 1_000); 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 IRavenQueryable WhereIf(this IRavenQueryable 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) { if (String.IsNullOrWhiteSpace(searchTerms)) { return source; } searchTerms = searchTerms.Trim(); string[] searchParts = searchTerms.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => { if (suffix != null) { x += suffix; } if (prefix != null) { x = prefix + x; } return x; }).ToArray(); if (searchTerms.StartsWith('"') && searchTerms.EndsWith('"')) { searchParts = new[] { searchTerms }; } return source.Search(fieldSelector, searchParts, @operator: @operator); } } }