2015-06-21 41 views
1

我有以下LINQ查詢:如何指定行的範圍我的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(); 

回答

1

你可以使用skiptake,這樣的事情:

List<Person> people = db.People 
         .Skip(pageSize * pageNumber).Take(pageSize) 
         .OrderByDescending(t => t.Id) 
         .ToList(); 
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(); 
     } 
    }