Lookup<TKey,TElement>类型对象和分组是一样的,就好比使用Linq的group关键字后所查询出来的结果,使用foreach的时候,都可以用IGrouping<TKey,TElement>来迭代它们。Lookup<TKey,TElement>也是一种字典,不过它是一对多,不像Dictionary<TKey,TElement>一样是一对一的。Lookup<int,int>和Dictionary<int,List<int>>是一样的。

Lookup<TKey,TElement>的对象可以存储ToLookup方法的结果。代码如下:

class Package
{
public string Company;
public double Weight;
public long TrackingNumber;
} public static void LookupExample()
{
// Create a list of Packages to put into a Lookup data structure.
List<Package> packages = new List<Package> { new Package { Company = "Coho Vineyard", Weight = 25.2, TrackingNumber = 89453312L },
new Package { Company = "Lucerne Publishing", Weight = 18.7, TrackingNumber = 89112755L },
new Package { Company = "Wingtip Toys", Weight = 6.0, TrackingNumber = 299456122L },
new Package { Company = "Contoso Pharmaceuticals", Weight = 9.3, TrackingNumber = 670053128L },
new Package { Company = "Wide World Importers", Weight = 33.8, TrackingNumber = 4665518773L } }; // Create a Lookup to organize the packages. Use the first character of Company as the key value.
// Select Company appended to TrackingNumber for each element value in the Lookup.
Lookup<char, string> lookup = (Lookup<char, string>)packages.ToLookup(p => Convert.ToChar(p.Company.Substring(, )),
p => p.Company + " " + p.TrackingNumber); // Iterate through each IGrouping in the Lookup and output the contents.
foreach (IGrouping<char, string> packageGroup in lookup)
{
// Print the key value of the IGrouping.
Console.WriteLine(packageGroup.Key);
// Iterate through each value in the IGrouping and print its value.
foreach (string str in packageGroup)
Console.WriteLine(" {0}", str);
} // This code produces the following output:
//
// C
// Coho Vineyard 89453312
// Contoso Pharmaceuticals 670053128
// L
// Lucerne Publishing 89112755
// W
// Wingtip Toys 299456122
// Wide World Importers 4665518773 // Get the number of key-collection pairs in the Lookup.
int count = lookup.Count; // Select a collection of Packages by indexing directly into the Lookup.
IEnumerable<string> cgroup = lookup['C']; // Output the results.
Console.WriteLine("\nPackages that have a key of 'C':");
foreach (string str in cgroup)
Console.WriteLine(str); // This code produces the following output:
//
// Packages that have a key of 'C'
// Coho Vineyard 89453312
// Contoso Pharmaceuticals 670053128 // Determine if there is a key with the value 'G' in the Lookup.
bool hasG = lookup.Contains('G');

读书笔记 C# Lookup<TKey,TElement>和ToLookup方法的浅析的更多相关文章

  1. <读书笔记>软件调试之道 :实证方法

    有效调试不仅仅是排除缺陷,其包含如下几个步骤 弄明白软件为何运行错误 修复这个问题 避免破坏其它部分 保持或者提高代码的总体质量 确保同样的问题不在其它地方发生,也不会再次发生 构建实验.观察结果 依 ...

  2. 【读书笔记】iOS-GCD-系统提供的dispatch方法

    系统提供的dispatch方法如下: //系统提供的dispatch方法 //后台执行: dispatch_async(dispatch_get_global_queue(0, 0), ^{ // s ...

  3. [读书笔记]项目管理实战:Microsoft Project精髓与方法

    <项目管理实战:Microsoft Project精髓与方法>是Bonnie Biafore 写的一本书.Bonnie Biafore 作为项目管理师(PMP),她有20余年为大中小型客户 ...

  4. 锋利的jQuery读书笔记---jQuery中Ajax--get、post等方法

    load()方法通常用来从Web服务器上获取静态的数据文件,然而这并不能体现ajax的全部价值. 在项目中,如果需要传递一些参数给服务器中的页面,那么可以使用$.get()或者$.post()方法(或 ...

  5. Effective Java2读书笔记-对于所有对象都通用的方法(三)

    第12条:考虑实现Comparable接口 这一条非常简单.就是说,如果类实现了Comparable接口,覆盖comparaTo方法. 就可以使用Arrays.sort(a)对数组a进行排序. 它与e ...

  6. Effective Java2读书笔记-对于所有对象都通用的方法(二)

    第10条:始终要覆盖toString 这一条没什么好讲的,就是说默认的toString方法打印出来的是类名+@+十六进制哈希码的值.我们应该覆盖它,使它能够展示出一些更为详细清晰的信息,这个看实际情况 ...

  7. Effective Java2读书笔记-对于所有对象都通用的方法(一)

    第8条:覆盖equals时请遵守通用约定 ①约定的内容 自反性.对于任何非null的引用值x.x.equals(x)必须返回true. 对称性.对于任何非null的引用值x和y.当且仅当y.equal ...

  8. 读书笔记 C# 控制台应用程序之Main方法浅析

    Main方法是C#控制台应用程序和Windows窗体应用程序的入口点.Main方法可以有形参,也可以没有,可以有返回值(int整型),也可以没有.如下定义: 无返回值.无形参的格式: static v ...

  9. INSPIRED启示录 读书笔记 - 第26章 合理运用敏捷方法

    十大秘诀 1.产品经理即是产品负责人,他代表了客户的需求,因而需要与产品开发团队保持密切的联系,协助督促开发进程,及时解决出现的问题 2.使用敏捷方法绝不等于省略产品规划.规划周期应该适度缩短,反复迭 ...

随机推荐

  1. luogu3261 懒惰左偏树 [JLOI2015]城池攻占

    目录 题目 思路 错误&&反思 代码 题目 luogu 原来左偏树真的能懒惰下放 那这篇博客应该要咕咕了 一开始我按照那篇博客想了一下,感觉emm,还是瞄了一眼看到了pushdown ...

  2. JavaScript:Object属性方法

    Object的属性(firebug中没有找到) var pro={ city:"shanghai", list:[,,,,] } var Person=function(name, ...

  3. poj 2481 Cows(树状数组)题解

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  4. 整理ASP.NET MVC 5各种错误请求[401,403,404,500]的拦截及自定义页面处理实例

    http://2sharings.com/2015/asp-net-mvc-5-custom-404-500-error-hanlde https://blog.csdn.net/yhyhyhy/ar ...

  5. java自学入门心得体会 从环境配置开始

    java —— 一种可以撰写跨平台应用软件的面向对象的程序设计语言. 很多教程里都要概述java语言的诞生发明.其实像图灵的”图灵机“和”图灵测试“一样,当初的java并不是这样. 是用来操控一些电冰 ...

  6. 《C语言程序设计》指针篇<二>

    通过指针引用多维数组 如何理解二维数组元素的地址? 要知道,这本书用了整整两页的内容来讲解这方面的知识,从这里足以看出来理解通过指针来引用二维数组是一件比较麻烦的事情,但是我认为理解并不难. 什么是二 ...

  7. Java中创建只读容器,同步容器

    我们通过Collections.unmodifiableX来得到只读容器,因为容器被设为只读的,所以必须填入有意义的数据之后才进行设置 import java.util.ArrayList; impo ...

  8. C++ Boost在VS2015中的使用

    1.下载包 目录结构: 切换到上面的目录,然后运行 bootstrap.bat 执行完毕后会生成两个exe文件 继续执行 bjam.exe 结束后,目录如下 2.设置路径 测试 #include &q ...

  9. shell wc命令 统计行数

    users文件内容 hello world 我们要统计 users 文件的行数,执行以下命令: $ wc -l users users 也可以将输入重定向到 users 文件: $ wc -l < ...

  10. c++ 在指定长度的数组或者容器中,统计元素出现的次数(count)

    #include <iostream> // cout #include <algorithm> // count #include <vector> // vec ...