NET问答: 说说你对 Lookup<TKey, TElement> 的看法 ?

咨询区

  • dan-gph

MSND 上对 Lookup 做了如下的解释。

Lookup<TKey, TElement> 类似于 Dictionary<TKey,TValue>, 不同点在于 Dictionary<TKey, TValue> 中的key对应的是单个value,而 Lookup<TKey, TElement> 中的 key 对应的是一个 value集合

我觉得这个解释等于没解释,请问 Lookup 的用途在哪里?

回答区

  • bobbymcr

你可以把 Lookup<TKey, TElement>Dictionary<TKey, Collection<TElement>> 看作是等价的,很明显后者通过 key 可以返回与之匹配的 list 集合。


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

点评区

相信很多人对 Lookup 不是很理解,我初学时也没搞特别清楚,真不知道 Lookup 是出自哪里的术语,不过没关系,没听过 Lookup,总听过 GroupBy 吧,对,就是关系型数据库中用到的 group by,这两者是等价的,只不过又造了一个新名词而已,不信的话,我写个例子给你看看。


        static void Main(string[] args)
        {

            var nums = new int[] { 1, 2, 3, 3 };

            //使用 lookup
            ILookup<int, int> query = nums.ToLookup(t => t);

            foreach (IGrouping<int, int> item in query)
            {
                var key = item.Key;
                var list = item.ToList();

                Console.WriteLine($"lookup : key={key}, value={string.Join(",", list)}");
            }

            Console.WriteLine($"\r\n  -------------- \r\n");

            //使用 group
            IEnumerable<IGrouping<int, int>> query2 = nums.GroupBy(t => t);

            foreach (IGrouping<int, int> item in query2)
            {
                var key = item.Key;
                var list = item.ToList();

                Console.WriteLine($"groupby : key={key}, value={string.Join(",", list)}");
            }

            Console.ReadLine();
        }

从结果看,两者都做了相同的事情,仔细观察代码,你会发现在 foreach 的迭代项 item 都是 IGrouping<int, int> 类型,接下来就有一个疑问了,query 还能被 foreach 吗?这个要看 ILookup<int, int> 的内部迭代类是怎么写的了,翻一下代码看看。


public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IEnumerable, ILookup<TKey, TElement>
{
    public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
    {
        Grouping g = lastGrouping;

        if (g != null)
        {
            do
            {
                g = g.next;
                yield return g;
            }
            while (g != lastGrouping);
        }
    }
}

看到没有,迭代类返回的类型 IEnumerator<IGrouping<TKey, TElement>> 不正是做 GroupBy 的 query2 类型 IEnumerable<IGrouping<int, int>> 嘛 ~~~

如果你要不懂 sql 的 group by, 那就当我没说哈 ????????????

原文链接:https://stackoverflow.com/questions/1403493/what-is-the-point-of-lookuptkey-telement

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值