[英]How do I select the top ten scores in a database table full of scores using linq in C#
I'm new to Linq and database programming in general, I could really use some help. 我是Linq和数据库编程的新手,我真的可以使用一些帮助。
I have tried using 我尝试使用
var TopTen = from t in datacontext.Scores.Take(10)
orderby t.LifetimeScore descending
select t;
but this only seems to give me the first ten entries in the DB not the top ten. 但这似乎只给了我数据库中的前十个条目,而不是前十个。 I know I need to order the table before the search but I just can't figure it out.
我知道我需要在搜索之前对表格进行排序,但我无法弄清楚。
Thanks, any help is appreciated 谢谢,感谢您的帮助
You have to take 10 from the result, not before: 您必须从结果中减去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)
I'm new to LINQ myself but here's what I think should work: 我本人是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();
Last statement will make sure the query is executed . 最后一条语句将确保查询已执行 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.