为什么Linq GroupBy在OrderBy之后驳回了订单操作?
我有一个带有 Session 导航属性的 Action 模型,
请考虑以下代码:
var x=db.Actions.OrderBy(p => p.Session.Number).ThenBy(p => p.Date);//it's OK
x 是一个有序的 Action,但是当在 x 上分组时,组不会在 x 上迭代(基于 >Action.Session)手动处理有序枚举:
var y=x.GroupBy(p=>p.Session).ToArray()
y有一组(Key,IGrouping)会话,但为什么group.Key未排序基于Session.Number >?
如何按编号到达一组会话并按日期排序每个组?
I have a Action model with Session Navigation Property,
Consider this code:
var x=db.Actions.OrderBy(p => p.Session.Number).ThenBy(p => p.Date);//it's OK
x is a ordered Action, but when grouped on x, group not iterate on x(base on Action.Session) manually on ordered enumerable:
var y=x.GroupBy(p=>p.Session).ToArray()
y have a group(Key,IGrouping) of sessions but why group.Key not ordered base on Session.Number?
How to i reached a group of Session order by number and each group ordered by date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为它是
Enumerable.GroupBy
保持秩序。对于Queryable.GroupBy 没有做出此类承诺
。从前者的文档来看:
你说的是后者,上面没有提到。在
GroupBy
之后调用OrderBy
使其正常工作。更新:由于您显然希望不仅仅对 GroupBy 键进行排序,因此您应该能够使用另一个 GroupBy 重载来指定要对每个会话的操作列表进行排序:
Because it's
Enumerable.GroupBy
that preserves order. No such promise is made forQueryable.GroupBy
.From the documentation of the former:
You're calling the latter, and the above is not mentioned. Call
OrderBy
afterGroupBy
to make it work.Update: since you apparently want to sort on more than just the GroupBy key, you should be able to use another GroupBy overload to specify that each session's list of actions is to be sorted:
因为未定义 GroupBy 保留插入顺序或底层键顺序(与
Dictionay<,>
对于本地内存工作不提供此类保证的方式相同)。相反,只需在分组后进行排序:特别要注意的是,要直接翻译顺序将要求它分析表达式,以发现排序的哪些部分与分组重叠(以及哪些部分不重叠),这是非常重要的。
Because it is not defined that GroupBy preserves either insertion order or the underlying key order (in the same way that
Dictionay<,>
makes no such guarantee, for local in-memory work). Just order after grouping, instead:In particular, note that to translate the order directly would require it to analyse the expression to spot which parts of the ordering overlap with the grouping (and which don't), which is non-trivial.
感谢@Marc Gravell & @hvd 有关 groupby IGrouping(Of TKey, TElement) 的注释不保留 TKey 的顺序,但保留 TElement 的顺序。
因此,我对最后一个问题(如何按号码排序并按日期排序每个组?)的回答是:
Thanks to @Marc Gravell & @hvd for note about groupby IGrouping(Of TKey, TElement) not preserves order of TKey but preserves order of TElement.
So my answer for my final question (How to i reached a group of Session order by number and each group ordered by date?) is:
仅名称
GroupBy
就表明此时查询的数据将根据提供的参数进行分组、聚合(按照您想要的方式调用)到另一个数据单元中。一般来说,如果您想查看排序后的结果,则
Sort()
函数调用应该是按顺序的最后一个。Just the name
GroupBy
suggests that the data queried at that moment will be grouped, aggregated (call how you want) into another data unit based on parameter provided.In general if you want to see result
sorted
theSort()
function call should be the last one in sequence.