首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TElement>,Lookup<TKey有什么意义?

TElement>,Lookup<TKey有什么意义?
EN

Stack Overflow用户
提问于 2009-09-10 05:20:32
回答 5查看 97K关注 0票数 170

MSDN对Lookup的解释如下:

Lookup类似于Dictionary。不同之处在于Dictionary将键映射到单个值,而Lookup将键映射到值的集合。

我不认为这种解释有什么特别的帮助。Lookup的用途是什么?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-09-10 05:24:03

它是一个介于IGrouping和字典之间的混合体。它允许您通过一个键将项分组在一起,然后通过该键以一种有效的方式访问它们(而不是像GroupBy允许的那样,只是遍历所有项)。

例如,您可以加载.NET类型并按名称空间构建查找...然后非常容易地获取特定名称空间中的所有类型:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

public class Test
{
    static void Main()
    {
        // Just types covering some different assemblies
        Type[] sampleTypes = new[] { typeof(List<>), typeof(string), 
                                     typeof(Enumerable), typeof(XmlReader) };

        // All the types in those assemblies
        IEnumerable<Type> allTypes = sampleTypes.Select(t => t.Assembly)
                                               .SelectMany(a => a.GetTypes());

        // Grouped by namespace, but indexable
        ILookup<string, Type> lookup = allTypes.ToLookup(t => t.Namespace);

        foreach (Type type in lookup["System"])
        {
            Console.WriteLine("{0}: {1}", 
                              type.FullName, type.Assembly.GetName().Name);
        }
    }
}

(我通常会在普通代码中对这些声明中的大多数使用var。)

票数 231
EN

Stack Overflow用户

发布于 2009-09-10 05:24:53

考虑这个问题的一种方法是:Lookup<TKey, TElement>类似于Dictionary<TKey, Collection<TElement>>。基本上,可以通过相同的键返回一个包含零个或多个元素的列表。

代码语言:javascript
复制
namespace LookupSample
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();
            names.Add("Smith");
            names.Add("Stevenson");
            names.Add("Jones");

            ILookup<char, string> namesByInitial = names.ToLookup((n) => n[0]);

            // count the names
            Console.WriteLine("J's: {0}", namesByInitial['J'].Count()); // 1
            Console.WriteLine("S's: {0}", namesByInitial['S'].Count()); // 2
            Console.WriteLine("Z's: {0}", namesByInitial['Z'].Count()); // 0, does not throw
        }
    }
}
票数 67
EN

Stack Overflow用户

发布于 2013-07-08 19:21:05

Lookup的一个用途可能是逆转Dictionary

假设您有一个实现为Dictionary的电话簿,其中有一组(唯一的)姓名作为键,每个姓名都与一个电话号码相关联。但是两个名字不同的人可能会共享同一个电话号码。这对于Dictionary来说不是问题,因为它不关心两个键对应于相同的值。

现在,您需要一种查找给定电话号码属于谁的方法。您构建一个Lookup,添加来自您的Dictionary的所有KeyValuePairs,但是向后添加,将值作为关键字,并将关键字作为值。现在,您可以查询电话号码,并获得其电话号码为的所有人员的姓名列表。使用相同的数据构建Dictionary可能会丢失数据(或者失败,这取决于您是如何做到的),因为这样做

代码语言:javascript
复制
dictionary["555-6593"] = "Dr. Emmett Brown";
dictionary["555-6593"] = "Marty McFly";

表示第二个条目覆盖第一个条目-文档不再列出。

尝试以稍微不同的方式写入相同的数据:

代码语言:javascript
复制
dictionary.Add("555-6593", "Dr. Emmett Brown");
dictionary.Add("555-6593", "Marty McFly");

会在第二行抛出一个异常,因为你不能Add一个已经在Dictionary中的键。

当然,您可能希望使用其他一些单一数据结构在两个方向上进行查找,等等。这个示例意味着每次Dictionary发生更改时,您都必须从它重新生成Lookup。但对于某些数据来说,这可能是正确的解决方案。

票数 31
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1403493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档