简体   繁体   English

如何在C#中使用linq在充满分数的数据库表中选择前十个分数

[英]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.

相关问题 如何使用linq和lambdas从一个系列中获得前三名球员和他们的高分 - How can I get the top three players and their high scores from a collection using linq and lambdas 在不使用 c# 中的内置 function 的情况下,如何获得保龄球的最低和最高分数 - How would I get the Min and Max scores of the bowlers without using the built in function in c# 如何使用Linq在C#中删除列表中的重复项,同时保留重复项中得分最高的元素 - How to remove duplicates in a list while keeping elements with highest scores among duplicates, in C# with Linq 我如何在C#中使用LINQ“选择null作为”列” - How do I “select null as ”Column" Using LINQ in C# 如何在C#中使用LINQ选择单个实体 - How do I select a single entity using LINQ in C# 如何使用LINQ和动态查询定义SELECT TOP? - How do I define a SELECT TOP using LINQ with a dynamic query? 如何编辑 SOLR 如何对文档进行评分? - How do i edit how SOLR scores a document? 如何使用按钮将3个不同的乐谱添加到一个文本框中? - How do I use a button to add 3 different scores into one textbox? 如何编写球员得分的GUI-“历史”记录? - How do I code a GUI-“History” of a player's scores? 如何在 LINQ(C#) 中引用数据库表并将其作为 JsonResult 返回 - How do I reference a database table in LINQ(C#) and return it as a JsonResult
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM