public static PagedList <T> Create(IOrderedFindFluent <T, T> source, int pageNumber, int pageSize) { var count = (int)source.Count(); var items = source.Skip((pageNumber - 1) * pageSize).Limit(pageSize).ToList(); return(new PagedList <T>(items, count, pageNumber, pageSize)); }
public async IAsyncEnumerable <T> GetAsync <T>(Expression <Func <T, bool> > filter = null, Expression <Func <T, object> > orderBy = null, bool includeDeleted = false) where T : class { var collection = GetCollection <T>(); FilterDefinition <T> whereFilter = FilterDefinition <T> .Empty; FilterDefinition <T> deletedFilter = FilterDefinition <T> .Empty; if (filter != null) { whereFilter = new FilterDefinitionBuilder <T>().Where(filter); } if (!includeDeleted && typeof(T).IsInHierarchySubClassOf(typeof(BasePersistableEntity))) { deletedFilter = new FilterDefinitionBuilder <T>().Eq("Deleted", false); } var result = collection .Find(new FilterDefinitionBuilder <T>().And(whereFilter, deletedFilter)); IOrderedFindFluent <T, T> sortedResult = null; if (orderBy != null) { sortedResult = result.SortBy(orderBy); } foreach (var item in await(sortedResult ?? result).ToListAsync()) { yield return(item); } }
public override IEnumerable <TAggregateRoot> FindAll(Expression <Func <TAggregateRoot, bool> > specification, SortSpecification <TKey, TAggregateRoot> sortSpecification) { var find = this.collection.Find(specification); if (sortSpecification == null) { return(find.ToEnumerable().AsQueryable()); } var sorts = sortSpecification.Specifications.ToList(); if (sorts.Any(s => s.Item2 == SortOrder.Unspecified)) { return(find.ToEnumerable().AsQueryable()); } var firstSort = sorts[0]; IOrderedFindFluent <TAggregateRoot, TAggregateRoot> orderedFind = firstSort.Item2 == SortOrder.Ascending ? find.SortBy(firstSort.Item1) : find.SortByDescending(firstSort.Item1); for (var i = 1; i < sorts.Count; i++) { var current = sorts[i]; orderedFind = current.Item2 == SortOrder.Ascending ? orderedFind.SortBy(current.Item1) : orderedFind.SortByDescending(current.Item1); } return(orderedFind.ToEnumerable().AsQueryable()); }
public IAsyncEnumerable <T> GetAsync(Expression <Func <T, bool> > filter = null, Expression <Func <T, object> > orderBy = null, bool includeDeleted = false, params Expression <Func <T, object> >[] includes) { var collection = GetCollection(); FilterDefinition <T> whereFilter = FilterDefinition <T> .Empty; FilterDefinition <T> deletedFilter = FilterDefinition <T> .Empty; if (filter != null) { whereFilter = new FilterDefinitionBuilder <T>().Where(filter); } if (!includeDeleted && typeof(T).IsInHierarchySubClassOf(typeof(PersistableEntity))) { deletedFilter = new FilterDefinitionBuilder <T>().Eq("Deleted", false); } var result = collection .Find(new FilterDefinitionBuilder <T>().And(whereFilter, deletedFilter)); IOrderedFindFluent <T, T> sortedResult = null; if (orderBy != null) { sortedResult = result.SortBy(orderBy); } return((sortedResult ?? result) .ToEnumerable() .ToAsyncEnumerable()); }
public static IOrderedFindFluent <TEntity, TProjection> ThenBy(IOrderedFindFluent <TEntity, TProjection> orderedFind, string propertyName, Enums.SortDirectionEnum sortDirection) { propertyName.NotNullOrEmpty("propertyName"); var keySelector = GetKeySelector(propertyName); return(sortDirection == Enums.SortDirectionEnum.Ascending ? IFindFluentExtensions.ThenBy <TEntity, TProjection>(orderedFind, keySelector) : IFindFluentExtensions.ThenByDescending <TEntity, TProjection>(orderedFind, keySelector)); }
public static IOrderedFindFluent <TDocument> ThenByDescending <TDocument>( this IOrderedFindFluent <TDocument> find, Expression <Func <TDocument, object> > field) { var sb = Builders <TDocument> .Sort; var sort = sb.Combine(find.Options.Sort, sb.Descending(field)); return(find.Sort(sort)); }
private static IOrderedFindFluent <T, T> CallOrderByQueryable <T>(IOrderedFindFluent <T, T> query, string methodName, string propertyName) { if (string.IsNullOrEmpty(methodName)) { throw new ArgumentException("Empty method name"); } var param = Expression.Parameter(typeof(T), "x"); var property = Expression.Property(param, propertyName); var propertyInfo = typeof(T).GetProperty(propertyName); var typeForValue = propertyInfo.PropertyType; var body = propertyName.Split('.').Aggregate <string, Expression>(param, Expression.PropertyOrField); var funcType = typeof(Func <,>).MakeGenericType(typeof(T), typeof(object)); LambdaExpression lambda; if (typeForValue.IsClass == false && typeForValue.IsInterface == false) { lambda = Expression.Lambda(funcType, Expression.Convert(property, typeof(object)), param); } else { lambda = Expression.Lambda(funcType, property, param); } IOrderedFindFluent <T, T> result; switch (methodName) { case "SortBy": result = query.SortBy((Expression <Func <T, object> >)lambda); break; case "SortByDescending": result = query.SortByDescending((Expression <Func <T, object> >)lambda); break; case "ThenBy": result = query.ThenBy((Expression <Func <T, object> >)lambda); break; case "ThenByDescending": result = query.ThenByDescending((Expression <Func <T, object> >)lambda); break; default: result = query; break; } return(result); }
/// <summary> /// Gets the position of an element from a sequence based on a key /// </summary> public static async Task <int> IndexOf <T>(this IOrderedFindFluent <T, T> collection, Func <T, bool> func) { var list = await collection.ToListAsync(); for (var i = 0; i < list.Count; i++) { if (func.Invoke(list[i])) { return(i); } } return(-1); }
public static IOrderedFindFluent <TEntity, TEntity> OrderBy <TEntity>(this IFindFluent <TEntity, TEntity> findFluent, OrderCondition[] orderConditions) { IOrderedFindFluent <TEntity, TEntity> orderFindFluent = null; if (orderConditions == null || orderConditions.Length == 0) { findFluent = FindFluentSortBy <TEntity, TEntity> .OrderBy(findFluent, "Id", Enums.SortDirectionEnum.Ascending); } orderConditions.ForEach((e, i) => { orderFindFluent = i == 0 ? FindFluentSortBy <TEntity, TEntity> .OrderBy(findFluent, e.SortField, e.SortDirection) : FindFluentSortBy <TEntity, TEntity> .ThenBy(orderFindFluent, e.SortField, e.SortDirection); }); return(orderFindFluent); }
public async IAsyncEnumerable <T> GetAsync(Expression <Func <T, bool> >?filter = null, Expression <Func <T, object> >?orderBy = null, bool includeDeleted = false, params Expression <Func <T, object> >[] includes) #endif { var collection = GetCollection(); FilterDefinition <T> whereFilter = FilterDefinition <T> .Empty; FilterDefinition <T> deletedFilter = FilterDefinition <T> .Empty; if (filter != null) { whereFilter = new FilterDefinitionBuilder <T>().Where(filter); } if (!includeDeleted && typeof(T).IsInHierarchySubClassOf(typeof(PersistableEntity))) { deletedFilter = new FilterDefinitionBuilder <T>().Eq("Deleted", false); } var result = collection .Find(new FilterDefinitionBuilder <T>().And(whereFilter, deletedFilter)); IOrderedFindFluent <T, T>?sortedResult = null; if (orderBy != null) { sortedResult = result.SortBy(orderBy); } #if NETSTANDARD2_0 return((sortedResult ?? result) .ToEnumerable() .ToAsyncEnumerable()); #elif NETSTANDARD2_1 foreach (var item in await(sortedResult ?? result).ToListAsync().ConfigureAwait(false)) { yield return(item); } #endif }
public static IOrderedFindFluent <T, T> SortBy <T>(this IFindFluent <T, T> query, IList <QuerySorter> sorters) { IOrderedFindFluent <T, T> resultQuery = (IOrderedFindFluent <T, T>)query; if (sorters == null) { return(resultQuery); } if (sorters.Count == 0) { return(resultQuery); } bool isFirst = true; foreach (QuerySorter sorter in sorters) { string methodName = GetOrderByMethodName(sorter.SortOrder, isFirst); resultQuery = CallOrderByQueryable(resultQuery, methodName, sorter.PropertyName); isFirst = false; } return(resultQuery); }
Order <T>(IOrderedFindFluent <T, T> source) { return(source.ThenByDescending(Property.QueryFrom <T>())); }
/// <summary> /// Adds a descending field to the existing sort. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam> /// <param name="find">The fluent find.</param> /// <param name="field">The field.</param> /// <returns>The fluent find interface.</returns> public static IOrderedFindFluent <TDocument, TProjection> ThenByDescending <TDocument, TProjection>(this IOrderedFindFluent <TDocument, TProjection> find, Expression <Func <TDocument, object> > field) { Ensure.IsNotNull(find, nameof(find)); Ensure.IsNotNull(field, nameof(field)); find.Options.Sort = new SortDefinitionBuilder <TDocument>().Combine( find.Options.Sort, new DirectionalSortDefinition <TDocument>(new ExpressionFieldDefinition <TDocument>(field), SortDirection.Descending)); return(find); }
internal object Create(IOrderedFindFluent <PatientModel, ObservationModel[]> obs, string patientid) { throw new NotImplementedException(); }
public static IOrderedFindFluent <TDocument, TProjection> ThenBy <TDocument, TProjection>(this IOrderedFindFluent <TDocument, TProjection> collection, string fieldName, SortDirection direction) { var sortExpression = GetSortExpression <TDocument>(fieldName); collection = direction == SortDirection.Ascending ? collection.ThenBy(sortExpression) : collection.ThenByDescending(sortExpression); return(collection); }
/// <summary> /// Thens the by descending. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="source">The source.</param> /// <param name="field">The field.</param> /// <returns>The fluent find interface.</returns> public static IOrderedFindFluent <TDocument, TResult> ThenByDescending <TDocument, TResult>(this IOrderedFindFluent <TDocument, TResult> source, Expression <Func <TResult, object> > field) { Ensure.IsNotNull(source, "source"); Ensure.IsNotNull(field, "field"); var helper = new BsonSerializationInfoHelper(); helper.RegisterExpressionSerializer(field.Parameters[0], source.Collection.Settings.SerializerRegistry.GetSerializer <TResult>()); var sortDocument = new SortByBuilder <TResult>(helper).Descending(field).ToBsonDocument(); // this looks sketchy, but if we get here and this isn't true, then // someone is being a bad citizen. var currentSort = (BsonDocument)source.Options.Sort; currentSort.AddRange(sortDocument); return(source); }