Beispiel #1
0
        private async Task <IEnumerable <T> > Find <T>(ObjectSerializer Serializer, IMongoCollection <BsonDocument> Collection,
                                                       int Offset, int MaxCount, FilterDefinition <BsonDocument> BsonFilter, params string[] SortOrder)
        {
            IFindFluent <BsonDocument, BsonDocument> ResultSet = Collection.Find <BsonDocument>(BsonFilter);

            if (SortOrder.Length > 0)
            {
                SortDefinition <BsonDocument> SortDefinition = null;

                foreach (string SortBy in SortOrder)
                {
                    if (SortDefinition == null)
                    {
                        if (SortBy.StartsWith("-"))
                        {
                            SortDefinition = Builders <BsonDocument> .Sort.Descending(Serializer.ToShortName(SortBy.Substring(1)));
                        }
                        else
                        {
                            SortDefinition = Builders <BsonDocument> .Sort.Ascending(Serializer.ToShortName(SortBy));
                        }
                    }
                    else
                    {
                        if (SortBy.StartsWith("-"))
                        {
                            SortDefinition = SortDefinition.Descending(Serializer.ToShortName(SortBy.Substring(1)));
                        }
                        else
                        {
                            SortDefinition = SortDefinition.Ascending(Serializer.ToShortName(SortBy));
                        }
                    }
                }

                ResultSet = ResultSet.Sort(SortDefinition);
            }

            if (Offset > 0)
            {
                ResultSet = ResultSet.Skip(Offset);
            }

            if (MaxCount < int.MaxValue)
            {
                ResultSet = ResultSet.Limit(MaxCount);
            }

            IAsyncCursor <BsonDocument> Cursor = await ResultSet.ToCursorAsync();

            LinkedList <T>          Result = new LinkedList <T>();
            BsonDeserializationArgs Args   = new BsonDeserializationArgs()
            {
                NominalType = typeof(T)
            };

            while (await Cursor.MoveNextAsync())
            {
                foreach (BsonDocument Document in Cursor.Current)
                {
                    BsonDocumentReader         Reader  = new BsonDocumentReader(Document);
                    BsonDeserializationContext Context = BsonDeserializationContext.CreateRoot(Reader);

                    T Obj = (T)Serializer.Deserialize(Context, Args);
                    Result.AddLast(Obj);
                }
            }

            return(Result);
        }