来源:https://blog.csdn.net/shandian84/article/details/8677681
初次使用linq to sql 对于查询语句还不熟悉,特此记录下
查询分页方法(转)
///
/// 分页查询 + 条件查询 + 排序
///
/// 泛型
/// 每页大小
/// 当前页码
/// 总数量
/// 查询条件
/// 排序条件
/// 是否升序
/// IQueryable 泛型集合
public IQueryable<_dpsui_logs> LoadPage_log(int pageSize, int pageIndex, out int total, Expression> whereLambda, Func<_dpsui_logs, Tkey> orderbyLambda, bool isAsc)
{
total = MyBaseDbContext.Set().Where(whereLambda.Compile()).Count();
if (isAsc)
{
var temp = MyBaseDbContext.Set<_dpsui_logs>().Where(whereLambda.Compile())
.OrderBy<_dpsui_logs, Tkey>(orderbyLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize);
return temp.AsQueryable();
}
else
{
var temp = MyBaseDbContext.Set().Where(whereLambda.Compile())
.OrderByDescending<_dpsui_logs, Tkey>(orderbyLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize);
return temp.AsQueryable();
}
}
使用多条件查询帮助类需要修改的地方
MyBaseDbContext.Set().Where(whereLambda)
修改为:
MyBaseDbContext.Set().Where(whereLambda.Compile())
多条件查询帮助类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace DPS
{
public static class DynamicLinqExpressions
{
//多条件查询帮助类
public static Expression> True() { return f => true; }
public static Expression> False() { return f => false; }
//注意this
public static Expression> Or(this Expression> expr1,
Expression> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
return Expression.Lambda>
(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression> And(this Expression> expr1,
Expression> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
return Expression.Lambda>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}
}
对于上面的修改做了下修改,由于多条件查询用到了上面的连接的方法。在执行时报错了,于是在网上再到了如下解决办法
来源:https://www.cnblogs.com/fengri/p/3376431.html
修改后运行正常
正常调用
var eps = DynamicLinqExpressions.True<_dpsui_logs>();// 多条件查询集合
DateTime date =DateTime.Parse( dateTimeInput1.Value.ToString("yyyy-MM-dd "));
if (date.ToString().Split('/')[0] != "0001")
{
//关于时间 由于需求是查询特定某一天的数据。数据库中 时间字段包含 00:00:00
//于是转换了下
eps=eps.And(u =>u.time.Value.ToString("yyyy-MM-dd ") == date.ToString("yyyy-MM-dd "));
}
string username = txtuserName.Text;
if (username != "")
{
eps = eps.And(u => u.username == username);
}
string opt = comboBox1.SelectedItem.ToString();
if (opt != "请选择")
{
eps = eps.And(u => u.opt == opt);
}
int pagesize = int.Parse(cmd_pagecount.SelectedItem.ToString());//每页显示多少条数据
int count = 0;//返回的页数
//调用分页
var log = dPShelper.LoadPage_log(pagesize, CurrentPage, out count, eps, u => u.id, true);
txtpage.Text = count.ToString();//有多少条数据
txtpagesizeCount.Text = string.Format("{0}", (count + 1) / pagesize + 1); ;//得到总页数
txtpagesize.MaxValue = int.Parse(txtpagesizeCount.Text);
DGV1.AutoGenerateColumns = false;
DGV1.DataSource = log.ToList();//绑定数据的时候要 toLisst() 不然会报异常
由于要查询两张表A和B,
要根据A中的ID查询B表中相应的数量
所以更新了下分页
///
/// card_A 是自己定义的实体类
///
///
//已发卡查询
public IQueryable LoadPage_dpcinfo(int pageSize, int pageIndex, out int total, Expression> whereLambda, Func orderbyLambda, bool isAsc)
{
total = MyBaseDbContext.Set().Where(whereLambda).Count();
if (isAsc)
{
var temp = MyBaseDbContext.Set()
.Where(whereLambda)
.OrderBy(orderbyLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize)
.GroupJoin(MyBaseDbContext.cardda_mxz, d => d.dpcid, c => c.dpcid, (d, c) => new Log.card_A { dpcid = d.dpcid, addr = d.addr, count_add = c.Count() }) ;
return temp.AsQueryable();
}
else
{
var temp = MyBaseDbContext.Set()
.OrderByDescending(orderbyLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize)
.GroupJoin(MyBaseDbContext.cardda_mxz, d => d.dpcid, c => c.dpcid, (d, c) => new Log.card_A { dpcid = d.dpcid, addr = d.addr, count_add = c.Count() });
return temp.AsQueryable();
}
}
注意:Log.card_A是自己定义的实体类。根据界面上需要显示的数据源定义的一个实体类
绑定数据
做了个查询优化
1、当用户查询 刷新时,从当前页开始刷新,不会跳转到第一页
2、当显示行数增加,触发查询时,如当前页*显示页数超过数据总量时,自动显示最后一页
反之正常显示当前页数
public void GOselect_card()
{
int pagesize = int.Parse(txtpageNow_card.Text);//当前页数
int pagesizeCount = int.Parse(txtpagesizeCount_card.Text);//总页数
int pagecount = int.Parse(cmd_pagecount_card.SelectedItem.ToString());//显示多少条
int count_user = int.Parse(txtpage_card.Text);//总条数
if (pagesize > 0 || pagesize <= pagesizeCount)
{
if ((pagesize * pagecount) > count_user && count_user != 0)
{
txtpageNow_card.Value = int.Parse(txtpagesizeCount_card.Text);//最大页数
pagesizeCount = (count_user - 1) / pagecount + 1;//得到总页数
selectcard(pagesizeCount);
}
else
{
selectcard(pagesize);
}
}
}