コード例 #1
0
ファイル: MongoView.cs プロジェクト: shi-tou/MyMongoDB
        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            Hashtable hs = new Hashtable();

            switch (currentCollection)
            {
            case "ErrorLog":
                PagingList <ErrorLog> pagingList1 = service.PageList <ErrorLog>(hs, this.pagerControl1.PageIndex, this.pagerControl1.PageSize, "CreateTime", true);
                this.dataGridView1.DataSource = pagingList1.ReturnData;
                pagerControl1.DrawControl((int)pagingList1.TotalCount);
                break;

            default:
                break;
            }
        }
コード例 #2
0
ファイル: MongoService.cs プロジェクト: shi-tou/MyMongoDB
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="hs">条件</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortFiled">排序字段</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        public PagingList <T> PageList <T>(Hashtable hs, int pageIndex, int pageSize, string sortFiled, bool isAsc)
        {
            IMongoCollection <T> collection = database.GetCollection <T>(typeof(T).Name);
            var builder = Builders <T> .Filter;
            var filter  = builder.Empty;

            foreach (string key in hs.Keys)
            {
                filter &= builder.Eq(key, hs[key]);
            }
            var sort = isAsc ? Builders <T> .Sort.Ascending(sortFiled) : Builders <T> .Sort.Descending(sortFiled);

            PagingList <T> result = new PagingList <T>();

            result.TotalCount = collection.Count(filter);
            result.ReturnData = collection.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList();
            return(result);
        }