时间:2021-07-01 10:21:17 帮助过:14人阅读
对这四大方法的实现,为了不让MongoDB的更多细节公开,所以,更多的内核只能在仓储中实现了,呵呵
public PagedResult<TEntity> GetModel(int pageIndex, int pageSize) { return GetModel(i => true, pageIndex, pageSize); } public PagedResult<TEntity> GetModel(System.Linq.Expressions.Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize) { return GetModel(expression, new Dictionary<Expression<Func<TEntity, object>>, bool>(), pageIndex, pageSize); } public PagedResult<TEntity> GetModel( Dictionary<Expression<Func<TEntity, object>>, bool> fields, int pageIndex, int pageSize) { return GetModel(i => true, fields, pageIndex, pageSize); } public PagedResult<TEntity> GetModel( Expression<Func<TEntity, bool>> expression, Dictionary<Expression<Func<TEntity, object>>, bool> fields, int pageIndex, int pageSize) { SortDefinition<TEntity> sorts = new ObjectSortDefinition<TEntity>(new { }); foreach (var item in fields) { if (item.Value) sorts = sorts.Ascending(item.Key); else sorts = sorts.Descending(item.Key); } var skip = (pageIndex - 1) * pageSize; var limit = pageSize; var recordCount = _table.CountAsync<TEntity>(i => true).Result; return new PagedResult<TEntity>( recordCount, (int)(recordCount + pageSize - 1) / pageSize, pageSize, pageIndex, _table.Find(expression) .Sort(sorts) .Skip(skip) .Limit(limit) .ToListAsync().Result); }
OK,现在你可以在业务层去调用它了,基本上面四个方法可以满足你的一切需求了,呵呵!
看下在UI层对它的调用,本DEMO没有BLL层,呵呵
// // GET: /Background/ public ActionResult Index(int sort = 0, int page = 1) { Dictionary<Expression<Func<WebManageUsers, object>>, bool> sortList = new System.Collections.Generic.Dictionary<Expression<Func<WebManageUsers, object>>, bool>(); sortList.Add(i => i.LoginName, sort == 0); var model = _webManageUsersRepository.GetModel(sortList,page, 10); return View(model); }
怎么样,看了上面代码是否心里痒痒了,哈哈,赶快去开发你自己的MongoDB仓储吧!
MongoDB学习笔记~为IMongoRepository接口添加了排序和表达式树,针对官方驱动
标签: