使用linq和ToLookup()保留值的顺序 [英] Preserve order of values with linq and ToLookup()

查看:42
本文介绍了使用linq和ToLookup()保留值的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我可以肯定从linq的ToLookup方法返回时键是无序的,但是否保留了的顺序?我找不到任何一种说明方式的文件.

While I'm pretty certain that the keys are unordered when returned from linq's ToLookup method, Is the order of values preserved? I can't find any documentation that says one way or the other.

推荐答案

ToLookup()的当前实现确实确实保留了值的顺序(请检查Lookup<TKey,TElement>.Grouping<TKey,TElement>.Add()的实现),但是我不认为可以保证保持这种方式.为了保证排序,最好的选择是使用Select带有索引的超载,然后再次排序.

The current implementation of ToLookup() does indeed preserve the order of the values (check the implementation of Lookup<TKey,TElement>.Grouping<TKey,TElement>.Add()), but I do not believe it is guaranteed to stay that way. To guarantee ordering, your best bet is probably to include the element's original index, perhaps using Select's with-index overload, and then sort again.

这篇关于使用linq和ToLookup()保留值的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆