Created
March 31, 2015 22:51
-
-
Save mizrael/5194187f4ee0a1ca6994 to your computer and use it in GitHub Desktop.
This file contains 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 PagedCollection<TEntity> Read<TEntity>(string collectionName, | |
IMongoQuery query, IMongoSortBy sortBy, IMongoFields fields, | |
int page, int pageSize) | |
{ | |
var connectionString = "....."; | |
MongoDatabase db = Connect(connectionString); | |
MongoCollection coll = db.GetCollection<TEntity>(collectionName); | |
MongoCursor<TEntity> foundItems = (null == query) ? coll.FindAllAs<TEntity>() : coll.FindAs<TEntity>(query); | |
if(null != fields) | |
foundItems = foundItems.SetFields(fields); | |
if (null != sortBy) | |
foundItems = foundItems.SetSortOrder(sortBy); | |
var result = new PagedCollection<TEntity>(){ | |
CurPage = page | |
}; | |
int skip = Math.Max(0, page) * pageSize; | |
result.Items = foundItems.SetSkip(skip) | |
.SetLimit(pagingSearcher.PageSize) | |
.ToArray(); | |
result.TotalCount = foundItems.Count(); | |
result.TotalPages = (long)Math.Ceiling((double)((float)result.TotalCount / pageSize)); | |
return result; | |
} |
This file contains 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 PagedCollection<T>{ | |
public IEnumerable<T> Items; | |
public long TotalCount; | |
public long TotalPages; | |
public long CurrPage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment