EF执行SQL分页查询语句

时间:2020-11-29 04:29:54
最近做一个功能,查询视图直接用EF的linq语句查询出来显示的数据是好多是重复的数据,后来就在网上找了下方法。

在这里拿出来给大家分享下,希望对大家有帮助,有什么不对或者不好的地方欢迎大家指出来,以为是第一次写希望大家多多包含

public List<VStudent> GetOrderDatas(int index, int count, out int totCount, DateTime? starTime, DateTime? endTime)
        {
            List<VStudent> list = null;
            totCount = 0;
            using (CET_Entities context = new CET_Entities())
            {
                string num = "select count(*) as num from VStudent where CreateTime >'" + starTime + "' and " + "CreateTime < '" + endTime + "'";//按照时间查询来显示数据量
                string query = "SELECT TOP " + count + " * FROM (SELECT ROW_NUMBER() OVER (ORDER BY CreateTime) AS RowNumber,* FROM VStudent where CreateTime >'" + starTime + "' and " + "CreateTime < '" + endTime + "') A WHERE RowNumber > " + count + "*( " + index +"-1"+ ")";//分页查询数据,按照一页10跳数据显示出来
                totCount = context.Database.SqlQuery<int>(num).FirstOrDefault();
                list = context.Database.SqlQuery<VStudent>(query).ToList();
            }
            return list;
        }