我有以下 LINQ 查询:
List<Person> people =
db.People.Take(pageSize)
.OrderByDescending(t => t.Id)
.ToList();
目标是指定应获取的行范围。我想用where
声明来做到这一点。问题是如何将行的特定索引放入where
语句中?
像这样:
List<Person> people =
db.People.Take(pageSize)
.Where(t => t.startIndex > 55 and t => t.endIndex < 60)
.OrderByDescending(t => t.Id)
.ToList();
您可以使用
skip
和take
,如下所示:
List<Person> people = db.People
.Skip(pageSize * pageNumber).Take(pageSize)
.OrderByDescending(t => t.Id)
.ToList();
public static List<Person> GetPeopleRange(int pageSize, int startIndex, int endIndex)
{
using (var db = new MyEntities())
{
return (from p in db.People
.Where(p => p.Id >= startIndex && p.Id < endIndex)
select p).Take(pageSize).OrderByDescending(p => p.Id).ToList();
}
}