什么是创建独特的数据结构更好的方法:HashSet或Linq的Distinct()? [英] What's better for creating distinct data structures: HashSet or Linq's Distinct()?
问题描述
我想知道是否可以就哪种方法是创建一组独特元素的更好方法达成共识:C# HashSet
还是使用IEnumerable's .Distinct()
(这是Linq函数)?
I'm wondering whether I can get a consensus on which method is the better approach to creating a distinct set of elements: a C# HashSet
or using IEnumerable's .Distinct()
, which is a Linq function?
假设我正在使用DataReader遍历数据库的查询结果,而我的选择是使用List
选项将我构造的对象添加到List<SomeObject>
或HashSet<SomeObject>
不得不做类似的事情:
Let's say I'm looping through query results from the DB with DataReader, and my options are to add the objects I construct to a List<SomeObject>
or to a HashSet<SomeObject>
With the List
option, I would wind up having to do something like:
myList = myList.Distinct().ToList<SomeObject>();
对于HashSet
,我的理解是,假设您已覆盖SomeObject中的GetHashCode()
和Equals()
方法,则向其中添加元素将自行处理非重复项.我主要关注这些选项的风险和性能方面.
With the HashSet
, my understanding is that adding elements to it takes care of the non-duplication by itself, assuming you've overrided the GetHashCode()
and Equals()
methods in SomeObject. I'm concerned mainly with the risks and performance aspects of the options.
谢谢.
推荐答案
更好"是一个难以理解的词-它对不同的人可能意味着许多不同的事物.
"Better" is a tricky word to use - it can mean so many different things to different people.
出于可读性考虑,我会选择Distinct()
,因为我个人认为这更容易理解.
For readability, I would go for Distinct()
as I personally find this more comprehensible.
为了提高性能,我怀疑手工制作的HashSet实现可能会稍微慢一些-但我怀疑它会非常不同,因为Distinct
的内部实现无疑会使用某种形式的哈希.
For performance, I suspect a hand-crafted HashSet implementation might perform mildly quicker - but I doubt it would be very different as the internal implementation of Distinct
will no doubt itself use some form of hashing.
对于我认为的最佳"实现...我认为您应该使用Distinct
,但是以某种方式将其推到数据库层-即在填充DataReader之前更改基础数据库SELECT.
For what I think of as "best" implementation... I think you should use Distinct
but somehow push this down to the database layer - i.e. change the underlying database SELECT before you fill the DataReader.
这篇关于什么是创建独特的数据结构更好的方法:HashSet或Linq的Distinct()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!