您的位置:首页 > 数据库

EF执行SQL分页查询语句

2014-01-10 11:01 330 查看
最近做一个功能,查询视图直接用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ASP.NET