Distinct() 方法是否保持序列的原始顺序不变? [英] Does Distinct() method keep original ordering of sequence intact?
问题描述
我想从列表中删除重复项,而不改变列表中唯一元素的顺序.
I want to remove duplicates from list, without changing order of unique elements in the list.
乔恩·斯基特 &其他人建议使用以下内容:
Jon Skeet & others have suggested to use the following:
list = list.Distinct().ToList();
参考:
能保证唯一元素的顺序和以前一样吗?如果是,请提供参考资料以证实这一点,因为我在文档中找不到任何相关内容.
Is it guaranteed that the order of unique elements would be same as before? If yes, please give a reference that confirms this as I couldn't find anything on it in documentation.
推荐答案
不能保证,但它是最明显的实现.很难以流式方式实现(即尽可能快地返回结果,尽可能少地读取) 不按顺序返回它们.
It's not guaranteed, but it's the most obvious implementation. It would be hard to implement in a streaming manner (i.e. such that it returned results as soon as it could, having read as little as it could) without returning them in order.
您可能想在 Distinct() 的 Edulinq 实现.
请注意,即使这对于 LINQ to Objects 是有保证的(我个人认为应该),但对于其他 LINQ 提供程序(例如 LINQ to SQL)来说也没有任何意义.
Note that even if this were guaranteed for LINQ to Objects (which personally I think it should be) that wouldn't mean anything for other LINQ providers such as LINQ to SQL.
在 LINQ to Objects 中提供的保证级别有时有点不一致,IMO.有些优化被记录在案,有些则没有.哎呀,有些文档完全错误.
The level of guarantees provided within LINQ to Objects is a little inconsistent sometimes, IMO. Some optimizations are documented, others not. Heck, some of the documentation is flat out wrong.
这篇关于Distinct() 方法是否保持序列的原始顺序不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!