public static IReadOnlyList <TestClass> StringASC_Skip_Control() { return(list .SortBy(n => n.Name) .Skip(_skip) .Limit(_limit) .ToList()); }
public static IOrderedAggregateFluent <T> OrderBy <T>(this IAggregateFluent <T> source, params IOrderVisitor[] visitors) { if (visitors.Length == 0) { return(source.SortBy(n => n)); } if (visitors.Length == 1) { return(visitors[0].Order(source)); } return(visitors.Skip(1).Aggregate ( visitors[0].Order(source), (collection, visitor) => visitor.Order(collection) )); }
public IDocumentAggregateSortedCursor <TResult> SortBy(Expression <Func <TResult, object> > property) { var next = _fluentAggregateCursor.SortBy(property); return(CreateNextStage(next)); }
/// <summary> /// Return establishment list. /// </summary> /// <param name="filter"></param> /// <returns></returns> public ListResult <EstablishmentListDTO> List(EstablishmentFilter filter) { IAggregateFluent <Establishment> establishmentAggregateFluent = this.MongoRepository .GetCollection <Establishment>().Aggregate(); if (filter.GeospatialQuery) { if (!filter.Meters.HasValue) { filter.Meters = 10000; } BsonDocument geoNearOptions = new BsonDocument { { "near", new BsonDocument { { "type", "Point" }, { "coordinates", new BsonArray { filter.Longitude.Value, filter.Latitude.Value } }, } }, { "maxDistance", filter.Meters }, { "includeLocs", "Location.Coordinates" }, { "distanceField", "Location.Distance" }, { "spherical", true } }; establishmentAggregateFluent = establishmentAggregateFluent.AppendStage( (PipelineStageDefinition <Establishment, Establishment>) new BsonDocument { { "$geoNear", geoNearOptions } } ); this.TelemetryClient.TrackEvent( EventNames.EstablishmentListLocation, new { filter.Latitude, filter.Longitude } ); } if (filter.EstablishmentType.HasValue) { establishmentAggregateFluent = establishmentAggregateFluent .Match(document => document.EstablishmentTypes.Contains(filter.EstablishmentType.Value)); this.TelemetryClient.TrackEvent( EventNames.EstablishmentListType, new { filter.EstablishmentType } ); } if (!string.IsNullOrEmpty(filter.Query)) { establishmentAggregateFluent = establishmentAggregateFluent.Match( document => document.Name.Contains(filter.Query) || document.Description.Contains(filter.Query) ); this.TelemetryClient.TrackEvent( EventNames.EstablishmentListQuery, new { filter.Query } ); } if (filter.DaysOfWeekEnum != null) { Expression <Func <Establishment, bool> > availabilityExpression = document => false; foreach (DayOfWeekEnum dayOfWeek in filter.DaysOfWeekEnum) { availabilityExpression = availabilityExpression.Or( document => document.Availabilities.Any( availability => availability.DayOfWeek == dayOfWeek || availability.CloseDayOfWeek == dayOfWeek ) ); } establishmentAggregateFluent = establishmentAggregateFluent.Match(availabilityExpression); this.TelemetryClient.TrackEvent( EventNames.EstablishmentListDaysOfWeek, new { filter.DaysOfWeek } ); } IAggregateFluent <BsonDocument> aggregateFluent = establishmentAggregateFluent.As <BsonDocument>(); aggregateFluent = filter.OrderType == OrderTypeEnum.Distance ? aggregateFluent.SortBy(document => document["Location"]["Distance"]) : aggregateFluent.SortByDescending(document => document["Relevance"]); if (!this.UserContext.EstablishmentId.HasValue) { this.RelevanceService.Register <Establishment, EstablishmentFilter>(filter); } IEnumerable <BsonDocument> documents = aggregateFluent .Skip((filter.Page - 1) * filter.PageSize) .Limit(filter.PageSize) .ToList(); IEnumerable <EstablishmentListDTO> establishments = documents.Select(document => new EstablishmentListDTO { Id = document["_id"].AsObjectId, EstablishmentTypes = document["EstablishmentTypes"].AsBsonArray.Select(x => (EstablishmentTypeEnum)x.AsInt32), ImageThumbnail = document["ImageThumbnail"]["Uri"].AsString, Name = document["Name"].AsString, Distance = document["Location"].AsBsonDocument.Contains("Distance") ? document["Location"]["Distance"].AsDouble : (double?)null } ).ToList(); return(new ListResult <EstablishmentListDTO>( establishments, aggregateFluent.Count().FirstOrDefault()?.Count ?? 0, filter )); }
/// <summary> /// Return event list. /// </summary> /// <returns></returns> public ListResult <EventListDTO> List(EventFilter filter) { IAggregateFluent <Event> eventAggregateFluent = this.MongoRepository.GetCollection <Event>().Aggregate(); if (filter.GeospatialQuery) { if (!filter.Meters.HasValue) { filter.Meters = 10000; } BsonDocument geoNearOptions = new BsonDocument { { "near", new BsonDocument { { "type", "Point" }, { "coordinates", new BsonArray { filter.Longitude.Value, filter.Latitude.Value } }, } }, { "maxDistance", filter.Meters }, { "includeLocs", "Location.Coordinates" }, { "distanceField", "Location.Distance" }, { "spherical", true } }; eventAggregateFluent = eventAggregateFluent.AppendStage( (PipelineStageDefinition <Event, Event>) new BsonDocument { { "$geoNear", geoNearOptions } } ); this.TelemetryClient.TrackEvent( EventNames.EventListLocation, new { filter.Latitude, filter.Longitude } ); } if (!string.IsNullOrEmpty(filter.EstablishmentId)) { eventAggregateFluent = eventAggregateFluent.Match( document => document.EstablishmentId == new ObjectId(filter.EstablishmentId)); this.TelemetryClient.TrackEvent( EventNames.EventListEstablishment, new { filter.EstablishmentId } ); } if (filter.Genre.HasValue) { eventAggregateFluent = eventAggregateFluent.Match( document => document.Genres.Contains(filter.Genre.Value)); this.TelemetryClient.TrackEvent( EventNames.EventListGenre, new { filter.Genre } ); } if (!string.IsNullOrEmpty(filter.Query)) { eventAggregateFluent = eventAggregateFluent.Match( document => document.Name.Contains(filter.Query) || document.Description.Contains(filter.Query) ); this.TelemetryClient.TrackEvent( EventNames.EventListQuery, new { filter.Query } ); } if (filter.DateFilter) { eventAggregateFluent = eventAggregateFluent.Match(document => ( (document.EndDate >= filter.StartDate && document.StartDate <= filter.StartDate) || (document.StartDate <= filter.EndDate && document.EndDate >= filter.EndDate) || (document.StartDate >= filter.StartDate && document.EndDate <= filter.EndDate) )); this.TelemetryClient.TrackEvent( EventNames.EventListDate, new { filter.StartDate, filter.EndDate } ); } if (!this.UserContext.EstablishmentId.HasValue) { this.RelevanceService.Register <Event, EventFilter>(filter); } IAggregateFluent <BsonDocument> aggregateFluent = eventAggregateFluent.Lookup( nameof(Establishment), "EstablishmentId", "_id", "Establishment" ).Unwind("Establishment"); aggregateFluent = filter.OrderType == OrderTypeEnum.Distance ? aggregateFluent.SortBy(document => document["Location"]["Distance"]) : aggregateFluent.SortByDescending(document => document["Relevance"]); IEnumerable <BsonDocument> documents = aggregateFluent .Skip((filter.Page - 1) * filter.PageSize) .Limit(filter.PageSize) .ToList(); IEnumerable <EventListDTO> events = documents.Select(document => new EventListDTO { Id = document["_id"].AsObjectId, Name = document["Name"].AsString, StartDate = document["StartDate"].ToUniversalTime(), EndDate = document["EndDate"].ToUniversalTime(), Genres = document["Genres"].AsBsonArray.Select(x => (GenreEnum)x.AsInt32), ImageThumbnail = document["ImageThumbnail"]["Uri"].AsString, Distance = document["Location"].AsBsonDocument.Contains("Distance") ? document["Location"]["Distance"].AsDouble : (double?)null, Establishment = new EventListEstablishmentDTO { Id = document["EstablishmentId"].AsObjectId, Name = document["Establishment"]["Name"].AsString, ImageThumbnail = document["Establishment"]["ImageThumbnail"]["Uri"].AsString, } } ).ToList(); return(new ListResult <EventListDTO>( events, aggregateFluent.Count().FirstOrDefault()?.Count ?? 0, filter )); }
Order <T>(IAggregateFluent <T> source) { return(source.SortBy(Property.QueryFrom <T>())); }