internal static Votes.Model.Page.PageInfo<T> GetPageData(int pageIndex, int pageSize, Func<T, bool> sqlWhere, Func<T, object> orderBy) {
            using (DataContext db = new DataContext(Votes.Common.Utility.Util.ConnectionString())) {
                //System.IO.StreamWriter sw = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/log.txt"), true);
                //db.Log = sw;
                var query = from data in db.GetTable<T>() select data;                //if (sqlWhere != null) {
                //    query.Where(sqlWhere);
                //}
                //if (orderBy != null) {
                //    query.OrderByDescending(orderBy);
                //}
                var list = query.Where(sqlWhere == null ? new Func<T, bool>(l => 1 == 1) : sqlWhere).OrderByDescending(orderBy == null ? new Func<T, object>(l => l) : orderBy);                Votes.Model.Page.PageInfo<T> page = new Votes.Model.Page.PageInfo<T>();
                page.TotalRecord = list.Count();//这里算总记录数时候 跟踪数据库发现sql语句没有带where条件
                page.PageIndex = pageIndex;
                page.PageSize = pageSize;
                page.PageCount = (int)Math.Ceiling((page.TotalRecord + 0.0) / pageSize);
                page.DataSource = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();//这里也是
                //sw.Dispose();
                //sw.Close();
//总共执行2条sql语句 都木有带条件 到底应该怎么写呢?
                return page;
             }
        }

解决方案 »

  1.   

    不过我感觉不用这样啊,linq是可以延迟加载的,分页不就是为了 读到每页的时候,才从 数据库中进行读取吗?
    继续关注 
      

  2.   

    linq查询为什么把简单问题复杂化呢 
    艹你大爷的
      

  3.   

     var list = query.Where(sqlWhere == null ? new Func<T, bool>(l => 1 == 1) : sqlWhere).OrderByDescending(orderBy == null ? new Func<T, object>(l => l) : orderBy);=>sqlWhere = sqlWhere == null ? new Func<T, bool>(l => 1 == 1) : sqlWhere; var list = query.Where(sqlWhere).OrderByDescending(orderBy == null ? new Func<T, object>(l => l) : orderBy);
      

  4.   

    没好用的话,就改 var query = from data in db.GetTable<T>() select data;var query = from data in db.GetTable<T>() select data;=》var query = from data in db.GetTable<T>()
                where sqlWhere
                select data;具体写法可能还要调整,只是大概意思
      

  5.   

    我调试发现是我的sqlWhere有问题 Func<Votes.Linq.Vote.VoteQuestion, bool> SqlWhere {
                get {
                    if (string.IsNullOrEmpty(this.GetSearchKey)) return null;
                    if (Votes.Common.Utility.Util.Decrypt(this.GetSearchKey).Equals("Subject")) {
                        return l => l.Subject == this.GetSearchValue;
                    }
                    return null;
                }
            }
    //这是sqlWhere的构建 麻烦看看怎么改
      

  6.   

    是不是我定义的这个属性 不等于
    query.Where(l=>l.xx == "oo" )里面的表达式?
      

  7.   

    http://www.cnblogs.com/happyhippy/archive/2010/01/26/1656961.html