如何指定LINQ查询应检索的行范围

时间:2015-06-21 23:53:26

标签: c# .net asp.net-mvc linq entity-framework

我有以下LINQ查询:

private int x1;
private int x2;
private int y1;
private int y2;

public Slope(int a, int b, int c, int d) {

    y2 = d;
    y1 = c;
    x2 = b;
    x1 = a;

    System.out.println(x1 - x2 / y1 - y2);
}

public static void main(String[] args) {
    Slope slope = new Slope(70, 80, 1, 2);
}

目标是指定应该获取的行范围。我想用List<Person> people = db.People.Take(pageSize) .OrderByDescending(t => t.Id) .ToList(); 语句来做。问题是如何将特定的行索引放入where语句?

这样的事情:

where

2 个答案:

答案 0 :(得分:1)

您可以使用skiptake,如下所示:

List<Person> people = db.People
                        .Skip(pageSize * pageNumber).Take(pageSize)
                        .OrderByDescending(t => t.Id)
                        .ToList();

答案 1 :(得分:0)

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();
        }
    }