Skip to content

Instantly share code, notes, and snippets.

@mizrael
Created March 31, 2015 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizrael/5194187f4ee0a1ca6994 to your computer and use it in GitHub Desktop.
Save mizrael/5194187f4ee0a1ca6994 to your computer and use it in GitHub Desktop.
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;
}
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