First() 会执行 OrderBy() 吗?

发布于 2024-08-24 22:56:05 字数 405 浏览 4 评论 0原文

之间的(渐近)性能是否有任何差异,

var a = Orders.OrderBy(order => order.Date).First()

var y = Orders.Where(order => order.Date == Orders.Min(x => x.Date)).ToList();

即 First() 是否会执行 OrderBy()?我猜不会。 MSDN 说通过 foreach och GetEnumerator 枚举集合,但措辞没有排除其他扩展名。

Is there any difference in (asymptotic) performance between

var a = Orders.OrderBy(order => order.Date).First()

and

var y = Orders.Where(order => order.Date == Orders.Min(x => x.Date)).ToList();

i.e. will First() perform the OrderBy()? I'm guessing no. MSDN says enumerating the collection via foreach och GetEnumerator does but the phrasing does not exclude other extensions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小嗷兮 2024-08-31 22:56:05

有几件事:

  • OrderBy() 从小到大排序,因此您的两个替代方案返回不同的元素
  • Where() 通常是惰性的,因此您的第二个表达式实际上并不执行此操作任何计算都没有 - 直到使用为止。
  • 原则上,相关行为取决于查询提供者。例如,您可能确实希望 sql-server linq 查询提供程序以不同于 IEnumerable 查询提供程序的方式处理此问题。查询提供程序可能会选择让“OrderBy”的返回值足够专业化,以便对其调用 First() 识别(在编译时或运行时)它正在有序枚举上运行,并且选择返回(第一个)最小元素,而不是排序。
  • 特别是对于 IEnumerable 提供程序,OrderBy 恰好返回一个枚举,每次检索第一个元素时,它都会对输入进行完全缓冲和排序 - 因此,在常见的基本情况中Linq-to-objects 情况下,OrderBy().First()OrderBy().ToArray() 相当。

请记住,linq 只是一堆函数名称 - 每个提供程序可能会选择以不同的方式实现这些名称,因此上述内容仅适用于 System.Linq IEnumerable 查询提供程序,而不一定适用于其他提供程序。

A few things:

  • OrderBy() orders from small to large, so your two alternatives return different elements
  • Where() is typically lazy, so your second expression doesn't actually do any computation at all - not until used.
  • In principle, the behavior in question depends on the query provider. For example, you might indeed expect the sql-server linq query provider to deal with this differently than the IEnumerable query provider. A query provider might choose to have the return value of "OrderBy" be sufficiently specialized such that calling First() on it recognizes (either at compile or run-time) that it's running on an ordered enumerable and instead of sorting, opts to return the (first) minimum element.
  • Specifically for the IEnumerable<T> provider, OrderBy happens to return an enumerable that fully buffers and sorts the input each time the first element is retrieved - so, in the common basic Linq-to-objects case, OrderBy().First() is comparable to OrderBy().ToArray().

Remeber that linq is just a bunch of function names - each provider may choose to implement these differently, so the above only holds for the System.Linq IEnumerable query provider, and not necessarily others.

旧伤还要旧人安 2024-08-31 22:56:05

First 将返回传递给它的 IEnumerable 的第一个条目。由于传递给 First 的 IEnumerable 是 OrderBy 的结果,因此您的问题可以改写为“OrderBy 有效吗”,是的,它确实有效。

First 无法推迟 OrderBy 的执行,因为它会立即返回结果。例如:

        var numbers = new int[] { 9, 3, 4, 6, 7 };

        var num = numbers.First();
        Console.WriteLine(num);

        num = numbers.OrderBy(i => i).First();
        Console.WriteLine(num);

        Console.ReadLine();

First will return the first entry of the IEnumerable passed to it. Since the IEnumerable passed to First is the result of OrderBy your question can be rephrased to "Does OrderBy work", and, yes it does.

First cannot defer the execution of OrderBy because it returns the result right away. For example:

        var numbers = new int[] { 9, 3, 4, 6, 7 };

        var num = numbers.First();
        Console.WriteLine(num);

        num = numbers.OrderBy(i => i).First();
        Console.WriteLine(num);

        Console.ReadLine();
忆沫 2024-08-31 22:56:05

First 方法将执行 OrderBy (也就是说,当然是执行 First 方法)。当 First 方法从 OrderBy 结果中提取第一个项目时,它必须对所有项目进行排序以找出哪个是第一个。

根据查询的运行位置和方式(即,如果查询引擎无法围绕它进行优化),第二个查询的性能可能会非常糟糕。如果为 Orders 中的每个项目计算一次 Orders.Max,它就会变成 O(n*n) 操作,这是非常糟糕的。

还有一个功能差异,如果存在重复的日期,第二个查询可以返回多个项目。

The First method will perform the OrderBy (that is, given that the First method is executed of course). When the First method pulls the first item from the result of the OrderBy, it will have to sort all the items to find out which one is the first one.

Depending on where and how the query is run (i.e. if the query engine can't optimise around it), the second query can perform quite badly. If Orders.Max is evaluated once for each item in Orders, it becomes an O(n*n) operation, which is pretty bad.

There is a functional difference also, the second query can return more than one item if there are duplicate dates.

你的他你的她 2024-08-31 22:56:05

事实并非如此。话虽这么说,orderby 自然会在有人尝试实际获取第一个元素时执行。

但正如你所说,条件可能会进一步确定。因此,不 - 它当时不会执行。

It does not. THAT BEING SAID - naturally the orderby will execute the moment someone tries to actually GET the first element.

But as you said, the conditions may be further defined. As such, no - it does not execute at that moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文