Example #1
0
        public static IOrderedFindFluent <TDocument> OrderByDescending <TDocument>(
            this IFindFluentSource <TDocument> source,
            Expression <Func <TDocument, object> > field)
        {
            var find = source.ToFindFluent();

            return(find.SortByDescending(field));
        }
        public static IOrderedFindFluent <TDocument> SortBy <TDocument>(
            this IFindFluentSource <TDocument> source,
            Expression <Func <TDocument, object> > field)
        {
            var find = source.ToFindFluent();
            var sort = Builders <TDocument> .Sort.Ascending(field);

            return(find.Sort(sort));
        }
Example #3
0
        public static IFindFluent <TDocument> Where <TDocument>(
            this IFindFluentSource <TDocument> source,
            Expression <Func <TDocument, bool> > field)
        {
            var find = source.ToFindFluent();
            var fb   = Builders <TDocument> .Filter;

            find.Filter = fb.And(find.Filter, fb.Where(field));
            return(find);
        }
        public static async Task <TDocument?> FirstOrDefaultAsync <TDocument>(
            this IFindFluentSource <TDocument> source,
            CancellationToken cancellationToken = default)
        {
            var find   = source.ToFindFluent();
            var cursor = await find.Limit(1).ToCursorAsync(cancellationToken);

            await cursor.MoveNextAsync(cancellationToken);

            return(cursor.Current.FirstOrDefault());
        }
        public static async Task <List <TDocument> > ToListAsync <TDocument>(
            this IFindFluentSource <TDocument> source,
            CancellationToken cancellationToken = default)
        {
            var find = source.ToFindFluent();

            var cursor = await find.ToCursorAsync(cancellationToken);

            var result = new List <TDocument>();

            while (await cursor.MoveNextAsync(cancellationToken))
            {
                result.AddRange(cursor.Current);
            }

            return(result);
        }
        public static async Task <TDocument?> SingleOrDefaultAsync <TDocument>(
            this IFindFluentSource <TDocument> source,
            CancellationToken cancellationToken = default)
        {
            var find = source.ToFindFluent();

            IAsyncCursor <TDocument> cursor;

            if (!find.Options.Limit.HasValue || find.Options.Limit.Value > 2)
            {
                cursor = await find.Limit(2).ToCursorAsync(cancellationToken);
            }
            else
            {
                cursor = await find.ToCursorAsync(cancellationToken);
            }

            await cursor.MoveNextAsync(cancellationToken);

            return(cursor.Current.SingleOrDefault());
        }