EntityFramework动态多条件查询与Lambda表达式树 +分页查询

来源:https://blog.****.net/shandian84/article/details/8677681

初次使用linq to sql 对于查询语句还不熟悉,特此记录下

查询分页方法(转)

 
        public IQueryable<_dpsui_logs> LoadPage_log<Tkey>(int pageSize, int pageIndex, out int total, Expression<Func<_dpsui_logs, bool>> whereLambda, Func<_dpsui_logs, Tkey> orderbyLambda, bool isAsc)
        {
            total = MyBaseDbContext.Set<keyou_dpsui_logs>().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<keyou_dpsui_logs>().Where(whereLambda.Compile())
                           .OrderByDescending<_dpsui_logs, Tkey>(orderbyLambda)
                           .Skip(pageSize * (pageIndex - 1))
                           .Take(pageSize);
                return temp.AsQueryable();
            }
        }
MyBaseDbContext.Set<keyou_dpsui_logs>().Where(whereLambda)
修改为:
MyBaseDbContext.Set<keyou_dpsui_logs>().Where(whereLambda.Compile())

多条件查询帮助类
public static class DynamicLinqExpressions
{
public static Expression<Func<T, bool>> True() { return f => true; }
public static Expression<Func<T, bool>> False() { return f => false; }

    //注意this
    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                         Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
    }
}

对于上面的修改做了下修改,由于多条件查询用到了上面的连接的方法。在执行时报错了,于是在网上再到了如下解决办法
来源:https://www.cnblogs.com/fengri/p/3376431.html
EntityFramework动态多条件查询与Lambda表达式树 +分页查询

修改后运行正常

正常调用

  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() 不然会报异常