Distinct()方法是否保持序列的原始顺序完整? [英] Does Distinct() method keep original ordering of sequence intact?

查看:159
本文介绍了Distinct()方法是否保持序列的原始顺序完整?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表中删除重复项,而不更改列表中唯一元素的顺序.

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 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.

IMO有时在LINQ中为对象提供的保证级别有些不一致.一些优化已记录在案,而其他则没有.哎呀,有些文档完全是错误.

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屋!

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