2009-07-15 104 views

回答

12

還有就是要做到這一點,我所知道沒有直接的方法的。您必須將整個查詢拖放到客戶端,然後從那裏您可以在行號中進行投影。作爲替代,您可以編寫一個使用ROW_NUMBER的存儲過程,然後將該過程從Linq打到SQL。

就你而言,唯一能夠做到這一點的方法就是客戶端。請記住,下面的聲明不會在服務器上這樣做,但會拉下你的整個表,並在客戶端索引...

using (var dc = new DataClasses1DataContext()) 
{ 
    var result = dc.Users 
     .AsEnumerable()  // select all users from the database and bring them back to the client 
     .Select((user, index) => new // project in the index 
     { 
      user.Username, 
      index 
     }) 
     .Where(user => user.Username == "sivey"); // filter for your specific record 

    foreach (var item in result) 
    { 
     Console.WriteLine(string.Format("{0}:{1}", item.index, item.Username)); 
    } 
} 
+0

謝謝,斯科特。所以我想你會ToList()它,然後IndexOf()? – 2009-07-15 17:13:13

3

您應該能夠使用Skip和Take擴展方法來完成此操作。

例如,如果你想第10行:

from c in customers 
      where c.Region == "somewhere" 
      orderby c.CustomerName 
      select new {c.CustomerID, c.CustomerName} 
      .Skip(9).Take(1); 
+3

我不認爲這是答案這個問題正在被問到,但它得到了提升,所以我將它留在這裏。 – 2009-07-15 17:09:10