Created
February 16, 2021 08:29
-
-
Save skalahonza/00e5eb3231ae7f8d89c5b2325dce6512 to your computer and use it in GitHub Desktop.
MongoDb driver queryable interface integration with AutoMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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