我们的查询数据的界面往往需要集成导出功能, 查询、导出的数据源及数据格式基本相同, 主要区别在于查询往往需要分页, 而导出是导出所有查询结果,不需要分页。 如果两个方法分开写就会有很多相同的代码, 下面的方法将两者合并形成一个通用方法
/// <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();
}
}
查询条件和结果集都是一样的, 这样就做到了方法通用.
/// <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();
}
}
查询条件和结果集都是一样的, 这样就做到了方法通用.