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. git如何生成指定两个commit之间的补丁

    答:git format-patch <base commit id>..<latest commit id> 如git log输出以下内容: commit 2222222 y ...

  2. 【SVN】Linux搭建SVN服务

    1.yum安装svn yum install -y subversion 日志打印 Loaded plugins: fastestmirror Determining fastest mirrors ...

  3. (转)Nuts and Bolts of Applying Deep Learning

    Kevin Zakka's Blog About Nuts and Bolts of Applying Deep Learning Sep 26, 2016 This weekend was very ...

  4. 《EMCAScript6入门》读书笔记——23.Module的加载实现

  5. vs2010中自动给函数或者类加上注释宏模板

    Sub AddFunComment() Dim DocSel As EnvDTE.TextSelection DocSel = DTE.ActiveDocument.Selection DocSel. ...

  6. The way to Go(2): 语言的主要特性与发展的环境和影响因素

    Reference: Github: Go Github: The way to Go 语言的主要特性与发展的环境和影响因素 现有编程语言对于Go语言发展的影响: Why Go? C/C++ 的发展速 ...

  7. MVC ---- Lambda表达式

    Lambda表达式是比匿名函数还简洁的一种匿名方法语法 Lambda表达式缩写推演 new Func<string,int>(delegate(string str){return str ...

  8. Java中的垃圾回收机制

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  9. django 接口编写的配置

    一.修改settings文件 ALLOWED_HOSTS = ['*'] INSTALLED_APPS = [ 'corsheaders' ]    #加入该app 安装django-cors-hea ...

  10. 用NotePad++如何实现大小写转换

    1.小写转换大写  Ctrl + Shift + U 2.大写转换小写  Ctrl + U