IOrderedQueryable 和 IQueryable 之间有什么区别?
我有这个:
var points = from p in ContextDB.Points
orderby p.PointInTime descending
where p.InstanceID == instanceId
&& p.ParentPointID == null
&& p.PointTypeID == currentPointTypeID
select p;
和这个:
var points = from p in ContextDB.Points
where p.InstanceID == instanceId
&& p.ParentPointID == null
&& p.PointTypeID == currentPointTypeID
orderby p.PointInTime descending
select p;
虽然我了解两者的用法(并且其中一个稍后会生成错误),但我不明白它们有何不同。
我确实在 STO 的其他地方看到过类似的问题,但恐怕我还没有找到这个问题的答案。
I have this:
var points = from p in ContextDB.Points
orderby p.PointInTime descending
where p.InstanceID == instanceId
&& p.ParentPointID == null
&& p.PointTypeID == currentPointTypeID
select p;
and this:
var points = from p in ContextDB.Points
where p.InstanceID == instanceId
&& p.ParentPointID == null
&& p.PointTypeID == currentPointTypeID
orderby p.PointInTime descending
select p;
While I understand the use of both (and one generates an error later) I don't understand how they are different.
I did see questions like this elsewhere on STO, but I've not garnered what the answer to this question is, I'm afraid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现
IOrderedQueryable
的类型包含额外的状态来保存有关排序的信息。IQueryable
通常表示一个操作这将在稍后执行(并且可能在不同的计算机上使用完全不同的语言,例如使用 LINQ to SQL)。需要一个单独的接口,因为下一个操作可能是另一种排序,需要与第一个进行不同的处理(按列 A 然后按 B 排序需要定义两个 LINQ 运算符,但系统需要确保每个键都有助于整体排序)。A type implementing
IOrderedQueryable<T>
contains extra state to hold information about sorting.IQueryable<T>
normally represents an operation that will be performed later (and possibly in a completely different language on a different computer, e.g. with LINQ to SQL). A separate interface is needed because the next operation might be another sort, which needs to be treated differently to the first (to sort by column A and then B requires two LINQ operators to define, but the system needs to ensure that each key contributes to the overall sort).如果将查询理解转换为相应的 Linq 扩展方法,就很容易看出。 OrderBy() 的返回类型是 IOrderedEnumerable<>。 Where() 返回 IEnumerable<>。第一个表达式的作用是首先对所有点进行排序,然后仅选择与 where 子句匹配的点。最后应用的运算是Where,因此表达式类型是IEnumerable<>。
哪一种更高效很大程度上取决于您使用的 Linq 提供程序。我的钱是最后排序的。尽管我猜想提供商足够聪明,能够以最佳顺序对操作进行排序。你可能想检查一下。
It's easy to see if you translate the query comprehension to its corresponding Linq extension methods. The return type of OrderBy() is IOrderedEnumerable<>. Where() returns IEnumerable<>. What your first expression does is first sort all of the Points, then select only the ones that match the where clause. The last operation applied is Where, thus the expression type is IEnumerable<>.
Which one is more efficient depends largely on the Linq provider you use. My money is on sorting last. Although I'd guess that the provider is smart enough to order the operations in the best order. You might want to check that.
IOrderedQueryable 是产生特定顺序的查询结果,而 IQueryable 具有不确定顺序的元素。
IOrderedQueryable the result of a query that results in a specific order and IQueryable has elements in an indeterminate order.