简体   繁体   English

C#linq命令并采取

[英]C# linq order by and take

I'm doing a query in C# linq. 我正在用C#linq进行查询。

After selecting and saving the query i have to do sort it and take 15 rows... 选择并保存查询后,我必须对其进行排序,并采取15行...

var query = from a 
[...]
select new
{
    a.id,
    a.desc,
}
[...]

Are there any different between doing this: 这样做有什么不同:

return query.OrderByDescending(o => o.id).Take(15);

and this? 和这个?

return query.Take(15).OrderByDescending(o => o.id);

Thank you very much! 非常感谢你!

Yes, very much so. 是的,非常如此。 It's an order of operations type thing. 这是一种操作类型的东西。

Say you have a data set of 假设您有一个数据集

A
F
B
D
F
C

and you do a return query.Take(3).OrderBy(o => o.name); 然后你做一个return query.Take(3).OrderBy(o => o.name); you would get 你会得到的

A
B
F

However, if you do return query.OrderBy(o => o.name).Take(3); 但是,如果你确实return query.OrderBy(o => o.name).Take(3); then you would get 那你会得到的

A
B
C

The difference between the two queries is that the first one will produce the initial 15 items from the list ordered by id in descending order, while the second one will take the initial 15 items in "natural" order, re-order them in descending order by id , and return back to you. 两个查询之间的区别在于,第一个查询将按id按降序排序的列表生成最初的15个项目,而第二个查询将按“自然”顺序生成最初的15个项目,按降序重新排序通过id ,并返回给你。

Since the "natural" order in RDBMS is not guaranteed to remain the same across multiple requests, only the first query will return consistent results. 由于RDBMS中的“自然”顺序不能保证在多个请求中保持相同,因此只有第一个查询才会返回一致的结果。 In fact, the second query could potentially return entirely different record sets each time you invoke it. 实际上,每次调用时,第二个查询都可能返回完全不同的记录集。

Results of both queries would be consistent if you do them in-memory. 如果您在内存中执行这两个查询,结果将是一致的。

The difference is easy to illustrate with a simple example of 100 records with id s from 1 to 100, inclusive, ordered in ascending order by their ID. 这个差异很容易通过一个简单的例子来说明,其中100个记录的id为1到100(含),按ID按升序排序。 The first query would return records with IDs 100, 99, 98, ..., 86, while the second query would return records with IDs 15, 14, 13, ..., 1. 第一个查询将返回ID为100,99,98,...,86的记录,而第二个查询将返回ID为15,14,13,...,1的记录。

Yes, those two queries will return different results, as: 是的,这两个查询将返回不同的结果,如:

  • query.OrderByDescending(o => o.id).Take(15) will sort the whole source query and then return the first 15 items. query.OrderByDescending(o => o.id).Take(15)将对整个源查询进行排序,然后返回前15项。 This will result in the first 15 items across the whole sorted dataset you're using. 这将导致您正在使用的整个排序数据集中的前15个项目。
  • query.Take(15).OrderByDescending(o => o.id) will take the first 15 items in the query (in their current order) and just sort those first 15 items (so this is probably not what you want). query.Take(15).OrderByDescending(o => o.id)将获取查询中的前15个项目(按其当前顺序)并只排序前15个项目(因此这可能不是您想要的)。

Yes. 是。 One takes 15 and orders them. 一个需要15并订购它们。 The other order them and takes 15. 另一个订单他们需要15。

using System.Linq;

public static void Main(string[] args)
{
    var nums = Enumerable
        .Range(0, 50)
        .OrderBy(o => Guid.NewGuid().GetHashCode())  // poor mans shuffle
        .ToList(); 

    Console.WriteLine(string.Join(",", nums));

    Console.WriteLine(string.Join(",", nums.Take(15).OrderBy(i => i)));

    Console.WriteLine(string.Join(",", nums.OrderBy(i => i).Take(15)));

    Console.ReadLine();
}

Output: 输出:

6,32,22,18,9,11,5,33,0,24,1,42,38,30,21,20,23,2,36,8,15,12,29,47,46,19,49,44,4,3
    7,40,3,10,41,34,17,31,16,43,35,39,25,27,45,7,28,14,13,26,48

0,1,5,6,9,11,18,21,22,24,30,32,33,38,42 // nums.Take(15).OrderBy(i => i)

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14      // nums.OrderBy(i => i).Take(15)

Kindof simple to test ... 有点简单测试......

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

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