![](/img/trans.png)
[英]How can I get the top three players and their high scores from a collection using linq and lambdas
[英]How do I select the top ten scores in a database table full of scores using linq in C#
我是Linq和數據庫編程的新手,我真的可以使用一些幫助。
我嘗試使用
var TopTen = from t in datacontext.Scores.Take(10)
orderby t.LifetimeScore descending
select t;
但這似乎只給了我數據庫中的前十個條目,而不是前十個。 我知道我需要在搜索之前對表格進行排序,但我無法弄清楚。
謝謝,感謝您的幫助
您必須從結果中減去10,而不是之前:
var TopTen = (from t in datacontext.Scores
orderby t.LifetimeScore descending
select t).Take(10);
var TopTen = datacontext.Scores.OrderByDescending(t => LifetimeScore ).Take(10)
我本人是LINQ的新手,但我認為這應該可行:
var TopTen = (from t in datacontext.Scores
orderby t.LifetimeScore descending
select t).Take(10);
var TopTen = from t in datacontext.Scores
orderby t.LifetimeScore descending
select t;
TopTen = TopTen.Take(10).ToArray();
最后一條語句將確保查詢已執行 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.