2009-07-15 88 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