Skip to content

Instantly share code, notes, and snippets.

@skalahonza
Created February 16, 2021 08:29
Show Gist options
  • Save skalahonza/00e5eb3231ae7f8d89c5b2325dce6512 to your computer and use it in GitHub Desktop.
Save skalahonza/00e5eb3231ae7f8d89c5b2325dce6512 to your computer and use it in GitHub Desktop.
MongoDb driver queryable interface integration with AutoMapper
public static class MongoDbExtensions
{
public static IMongoQueryable<TDestination> ProjectTo<TSource, TDestination>(this IQueryable<TSource> query, AutoMapper.IMapper autoMapper) =>
query.ProjectTo<BookListDTO>(autoMapper.ConfigurationProvider) as IMongoQueryable<TDestination>;
}
// usage ...
private readonly IMongoCollection<Book> _books;
private readonly IMapper _mapper;
// ...
_books
.AsQueryable()
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ProjectTo<Book, BookListDTO>(_mapper)
.ToListAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment