c# - 什么时候返回 IOrderedEnumerable?

标签 c# .net linq semantics iorderedenumerable

是否应该将 IOrderedEnumerable 用作纯粹用于语义值的返回类型?

例如,在表示层消费模型时,我们如何知道集合是需要排序还是已经排序?

如果存储库使用 ORDER BY 子句包装存储过程,情况会怎样。存储库应该返回 IOrderedEnumerable 吗?如何实现?

最佳答案

我认为这不是个好主意:

Should IOrderedEnumerable be used as a return type purely for semantic value?

For example, when consuming a model in the presentation layer, how can we know whether the collection requires ordering or is already ordered?

如果您不知道序列是按哪个键排序的,那么知道它是有序的有什么意义呢? IOrderedEnumerable 接口(interface)的要点是能够添加次要排序标准,如果您不知道什么是主要标准,这就没有多大意义。

What about in the case that a repository wraps a stored procedure with an ORDER BY clause. Should the repository return IOrderedEnumerable? And how would that be achieved?

这没有意义。正如我已经说过的,IOrderedEnumerable 用于添加辅助排序条件,但是当存储过程返回数据时,它已经排序并且添加辅助排序条件为时已晚。您所能做的就是完全重新排序,因此对结果调用 ThenBy 不会产生预期的效果。

关于c# - 什么时候返回 IOrderedEnumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518557/

相关文章:

c# - 以编程方式使用附加字段扩展 Lucene 文档

javascript - 如何将 C# 函数中的字符串添加到元标记(引号内)?

c# - 如何将 T 类型的单个值转换为 IEnumerable<T>?

c# - 如何在 Unity 中将混音器的音量设置为 slider 的音量?

c# - 为什么 EF 导航属性返回 null?

c# - 如何获得枚举标志值的最短表示?

c# - 对数据表中的行进行排序

c# - 优化的 C# 代码。想要在没有嵌套循环的情况下使用更好的技术

c# - LINQ to Entities 无法识别 MVC5 中的方法 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' 方法错误

.net - 在同一个项目中结合 MVC + Blazor