从IEnumerable T列表中选择Distinct.在.NET 2.0中 [英] Select Distinct from a list of IEnumerable<T> in .NET 2.0

查看:78
本文介绍了从IEnumerable T列表中选择Distinct.在.NET 2.0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤重复值的IEnumerable对象,因此我想从中获取不同的值,例如,假设它保存了几天:

I am trying to filter an IEnumerable object of the duplicate values, so I would like to get the distinct values from it, for example, lets say that it holds days:

星期一 周二 周三 周三

monday tuesday wednesday wednesday

我想过滤它并返回:

星期一 周二 周三

.net 2.0中最有效的方法是什么?

What is the most efficient way to do this in .net 2.0?

推荐答案

Dictionary<object, object> list = new Dictionary<object, object>();
foreach (object o in enumerable)
    if (!list.ContainsKey(o))
    {
        // Do the actual work.
        list[o] = null;
    }

字典将使用哈希表来保存键,因此查找效率很高.

Dictionary will use a hash table to hold keys therefore lookup is efficient.

排序最多为O(n log(n)).具有高效哈希函数的哈希表通常胜过它(O(1)查找).

Sorting will be O(n log(n)) at best. A hash table with an efficient hash function often outperforms it (O(1) lookups).

这篇关于从IEnumerable T列表中选择Distinct.在.NET 2.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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