C# 驱动的mongodb的分页查询简单示例

 /// <summary>
        /// mongodb分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sortQuery"></param>
        /// <param name="query"></param>
        public void GetPaging<T>(Dictionary<string, string> sortQuery,  Expression<Func<T,bool>> query)
        {
            if (sortQuery.Count < 0)
            {
                return;
            }
            IMongoClient mongoClient = new MongoClient("");
            IMongoDatabase mongoDatabase = mongoClient.GetDatabase("");
            var sort = Builders<T>.Sort;
            SortDefinition<T> sortimp = null;
            foreach (var item in sortQuery)
            {
                if (item.Value == "d")
                {
                    sortimp = sort.Descending(item.Key);
                }
                else
                {
                    sortimp = sort.Ascending(item.Key);
                }
            }
            mongoDatabase.GetCollection<T>(typeof(T).Name).Find<T>(query).Sort(sortimp)?.Limit(1).Skip(1);
        }

 

posted @ 2018-06-13 09:53  洛晨随风  阅读(511)  评论(0编辑  收藏  举报