有一个 分页方法 如下:
#region 分页方法
/// <summary>
/// 分页
/// </summary>
/// <param name="where">条件</param>
/// <param name="order">排序</param>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">每页数据量</param>
/// <param name="count">总数</param>
/// <returns></returns>
[NonAction]
private List<ticket> getPageData(Func<ticket, bool> where, Func<ticket, dynamic> order, int pageIndex, int pageSize, out int count)
{
using (phoneTicketEntity db = new phoneTicketEntity())
{
var queryList = where != null ? db.tickets.AsNoTracking().Where(where) : db.tickets.AsNoTracking();
queryList = order != null ? queryList.OrderByDescending(order) : queryList;
count = queryList.Count();
return queryList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
#endregion
//前端调用getJsonData()返回json数据,这个方法调用了上面的分页功能,参数都是需要动态查询的字段,
//其中拼接了getPageData方法需要的where参数,显然是错误的,只能查询满足一个的条件,如何修改,请指点
public ActionResult getJsonData(int page = 1, int limit = 10,string username = "", string fpcode = "", string fpnumber = "",
string price_min="",string price_max="", string fpdate = "")
{
int count = 0;
Func<ticket, bool> where = t=> t.deptid == ((user)Session["user"]).ID; //只查当前部门
if (!string.IsNullOrEmpty(username))
where = t => t.username == username;
if (!string.IsNullOrEmpty(fpcode))
where = t => t.fpcode == fpcode;
if (!string.IsNullOrEmpty(fpnumber))
where = t => t.fpnumber == fpnumber;
if (!string.IsNullOrEmpty(price_min))
where = t => t.fpmoney >= Convert.ToDecimal(price_min);
if (!string.IsNullOrEmpty(price_max))
where = t => t.fpmoney <= Convert.ToDecimal(price_max);
if (!string.IsNullOrEmpty(fpdate))
where = t => t.fpdate.ToString() == fpdate;
//if (!string.IsNullOrEmpty(fpdatestart) && !string.IsNullOrEmpty(fpdateend))
// where = t => t.fpdate.AddDays //日期范围内
var tickets = getPageData(where,t => t.fpdate, page, limit, out count);
return Json(
new JsonFormatResult
{
Count = count,
Code = Code.ok,
Msg = "msg.ok",
Data = tickets
}, JsonRequestBehavior.AllowGet
);
}