如何編寫返回元素的ROWNUMBER與id ==的LINQ語句?LINQ語句返回ID爲==的東西的元素的rownumber?
11
A
回答
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));
}
}
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
1
如何投影行號插入LINQ查詢結果
How To Project a Line Number Into Linq Query Results
+3
在Linq to SQL中不支持Select的重載。會拋出一個NotSupportedException()。 – 2009-07-15 17:07:12
相關問題
- 1. if語句不返回任何東西
- 2. Linq的語句返回行
- 3. 返回語句不返回任何東西
- 4. 檢查如果SELECT語句返回的東西與IF
- 5. Mysqli SELECT語句的問題不會返回任何東西
- 6. Linq查詢不返回任何東西
- 7. LINQ to SQL返回語句
- 8. codeigniter選擇語句不返回任何東西
- 9. return語句在python遞歸中不返回任何東西
- 10. IF語句或更有效的東西?
- 11. SELECT語句返回ID
- 12. linq - 選擇不等於東西的元素
- 13. 獲取資源數組手動元素返回任何東西
- 14. 沒有「返回任何東西」的servlet的正確語法?
- 15. 類似於stdlib bsearch的東西,它立即返回更小的元素
- 16. mysql_fetch_array返回兩樣東西
- 17. 怎樣才能返回return語句不返回元素?
- 18. 返回鏈接列表中的東西
- 19. PHP:如何返回正確的東西
- 20. PHP PDO + MSSQL返回怪異的東西
- 21. 的getJSON不返回任何東西
- 22. WebClient的DownloadString不返回任何東西
- 23. kABPersonEmailProperty返回怪異的東西
- 24. 文檔化,只是返回的東西
- 25. 如何從AlamoFire返回的東西?
- 26. Flex 4 VGroup.getElementAt()返回元素的ID
- 27. 阿拉伯語字母Scrapy返回一些奇怪的東西
- 28. Linq語句爲所有條目返回相同的結果
- 29. 爲什麼遵循XPath語句返回所有「a」元素?
- 30. 生成n元素集的子集;函數不返回任何東西
謝謝,斯科特。所以我想你會ToList()它,然後IndexOf()? – 2009-07-15 17:13:13