c# linq的高级用法:分页查询和导出Excel共用一个方法

我们的查询数据的界面往往需要集成导出功能, 查询、导出的数据源及数据格式基本相同, 主要区别在于查询往往需要分页, 而导出是导出所有查询结果,不需要分页。 如果两个方法分开写就会有很多相同的代码, 下面的方法将两者合并形成一个通用方法
/// <summary>
        /// 获取数据源, 用于查询显示以及导出Excel
        /// </summary>
        /// <param name="pageable">是否分页查询</param>
        /// <param name="orderFiled">排序字段</param>
        /// <param name="desc">是否升序</param>
        /// <param name="rowCount">分页时返回总记录数, 不分页时返回-1</param>
        /// <returns>返回结果集</returns>
        private List<MyModel.FileBookModel> GetDataSource(bool pageable, string orderFiled, bool desc,out int rowCount)
        {
            //查询条件
            int searchKey;
            string searchString = this.toolStripTextBoxSearchKey.Text.Trim();
            bool searchStringIsInt = int.TryParse(searchString, out searchKey);
            if (this.toolStripTextBoxPageRows.Text != pageRows.ToString())
            {
                int newPageRows = 0;
                bool b = int.TryParse(this.toolStripTextBoxPageRows.Text, out newPageRows);
                if (b == false)
                    this.toolStripTextBoxPageRows.Text = pageRows.ToString();
                else
                    pageRows = newPageRows;
            }


            int year = this.toolStripComboBoxYear.SelectedIndex == 0 ? 0 : (int)this.toolStripComboBoxYear.SelectedItem;
            int month = this.toolStripComboBoxMonth.SelectedIndex == 0 ? 0 : (int)this.toolStripComboBoxMonth.SelectedItem;
            int fileTypeId = ((KeyValuePair<int, string>)this.toolStripComboBoxFileType.SelectedItem).Key;
            System.Linq.Expressions.Expression<Func<Model.FileInfo, bool>> where = (p => p.deleteFlag == 0 && (!filterPrintFlag || p.printFlag == 0)
                && (year == 0 || SqlFunctions.DatePart("year", p.receiveDate) == year)
                && (month == 0 || SqlFunctions.DatePart("month", p.receiveDate) == month)
                && (fileTypeId == 0 || p.fileTypeId == fileTypeId)
                && (searchString.Length == 0 || p.fileNo.Contains(searchString) || searchStringIsInt && p.id == searchKey || p.fileTitle.Contains(searchString) || p.sendCompany.Contains(searchString)));


            //结果集
            System.Linq.Expressions.Expression<Func<Model.FileInfo, MyModel.FileBookModel>> selector = (p => new MyModel.FileBookModel
                {
                    id = p.id,
                    fileType = p.Dictionary_FileType.value,
                    receiveDate = p.receiveDate,
                    sendCompany = p.sendCompany,// p.Dictionary_SendCompany.value,
                    fileNo = p.fileNo,
                    fileTitle = p.fileTitle,
                    securityLevel = p.Dictionary_SecurityLevel.value,
                    count = p.count,
                    nature = p.Dictionary_Nature.value,
                    undertake = p.undertake,
                    save = p.save,
                    memo = p.memo,
                    printFlag = p.printFlag == 1 ? "已打印" : "未打印",
                    approveHistoryCount = p.FileFlow.Count(pp => pp.deleteFlag == 0)//传输记录条数);
                });


            if (pageable)
            {
                //分页显示
                return Common.GetPageData<Model.FileInfo, object>(pageNo, pageRows, where, out rowCount, orderFiled, desc).Select(selector).ToList();


            }
            else
            {
                //导出, 不分页
                rowCount = -1;
                return Common.GetData<Model.FileInfo, object>(where, orderFiled, desc).Select(selector).ToList();
            }
        }
查询条件和结果集都是一样的, 这样就做到了方法通用.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值