简体   繁体   English

Linq-如何获得最高记录?

[英]Linq - how to get top records?

I have this code which queries a database 我有查询数据库的代码

var buildInfoList = (from m in context.BuildInfoes
                     where m.ManagerInfoGuid == managerGuid
                     select m).Take(3).ToList();

the code above gives me the first 3 results, how can i change it to take the last 3? 上面的代码为我提供了前3个结果,如何更改为后3个?

meaning if i have 100 rows in the database, i want to get 98, 99, 100 and not 1, 2, 3 意思是如果我在数据库中有100行,我想得到98、99、100而不是1、2、3

Reverse the order of the query. 颠倒查询顺序。 The basic idea is reverse the order of the entire query, fetch the first three elements, then reverse the order again to put them back in the right order: 基本思想是颠倒整个查询的顺序,获取前三个元素,然后再次颠倒顺序以将它们放回正确的顺序:

var query = from m in context.BuildInfoes
            where m.ManagerInfoGuid == managerGuid
            select m;
var lastItems = query.OrderByDescending(x => x.ID).Take(3).Reverse().ToList();

PS: If you were using Linq to Objects (but I guess you aren't) you could use TakeLast from morelinq. PS:如果您使用的是Linq to Objects(但我想不是),则可以使用TakeLast的TakeLast。

Your are not introducing any order here, so you currently get any 3 results which by chance don't happen to be the ones you want. 您在这里没有引入任何命令,所以您目前得到任何 3个结果,其偶然不正好是你想要的人。 Establish an order: 建立订单:

var buildInfoList = (from m in context.BuildInfoes
                     where m.ManagerInfoGuid == managerGuid
                     orderby m.Name descending
                     select m).Take(3).ToList();

Using orderby you can specify ascending or descending to reverse the order, which will result in returning the first or last 3 elements using Take . 使用orderby可以指定升序或降序来反转顺序,这将导致使用Take返回前三个元素。

You can use orderby 您可以使用orderby

var buildInfoList = (from m in context.BuildInfoes
                     where m.ManagerInfoGuid == managerGuid
                     orderby m.Id descending
                     select m).Take(3).ToList();

Or, as @MarkByers said, just use Reverse 或者,如@MarkByers所说,只需使用Reverse

var buildInfoList = from m in context.BuildInfoes
                    where m.ManagerInfoGuid == managerGuid
                    select m;
var count = buildInfoList.Count();
var list = buildInfoList.Skip(count < 3 ? count - 3 : 0).Take(3).ToList();

edit: Why is this solution different than the others? 编辑:为什么这种解决方案不同于其他解决方案? But this doesn't mean is the best one. 但这并不意味着是最好的。

First the OP states that the query is over a database and since the query uses Take without specifying the order, I guess is about Linq To Sql. 首先,OP声明查询是通过数据库进行的,并且由于查询使用Take而不指定顺序,因此我想这与Linq To Sql有关。

This solution is not actually the best because it does two queries, one for the count and the other for to get the items. 该解决方案实际上并不是最好的解决方案,因为它会执行两个查询,一个查询计数,另一个查询获取项目。 This solution uses only the SQL to get the last 3 items and doesn't do an order over objects. 此解决方案仅使用SQL来获取最后3个项目,并且不对对象进行排序。

While testing it with LINQ Pad I noticed that, when no order is specified, LINQ to SQL generates the order over all the columns 在使用LINQ Pad进行测试时,我注意到,当未指定任何顺序时,LINQ to SQL会在所有列上生成顺序

   SELECT ROW_NUMBER() OVER (ORDER BY [t0].[id], [t0].[A], [t0].[B], [t0].[C])

Obs.: 观测值:

The Reverse method is not translated, so is good to be called after a ToList() call Reverse方法未转换,因此最好在ToList()调用之后调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM