C#Distinct()方法是否保持序列的原始排序完整? [英] Does C# 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&其他人建议使用以下
Jon Skeet & others have suggested to use following
list = list.Distinct().ToList();
removing duplicates from a list C#
是否保证唯一元素的顺序与以前相同?如果是,请给出一个确认这一点的参考,因为我在文档中找不到任何东西。
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.
您可能想要阅读我在 Edulinq实现Distinct()。
请注意,即使这样保证了LINQ to Objects(其中我个人认为,它应该不会意味着LINQ to SQL等LINQ提供程序的任何东西。
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.
这篇关于C#Distinct()方法是否保持序列的原始排序完整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!