LinQ转换运算符ToDictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToDictionaryDemo
{
    class Program
    {
        public class Game
        {
            public string Opponent { get; set; }
            public string Score { get; set; }
        }
        static void Main(string[] args)
        {
            var spartans = new List<Game>
            {
                new Game{Opponent="UAB",Score="55-18"},
                new Game{Opponent="Bowling Green",Score="55-18"},
                new Game{Opponent="Pittsburgh",Score="55-18"},
                new Game{Opponent="Notre Dame",Score="55-18"}
            };
            //字典是一种键值对的集合,ToDictionary 将一个IEnumerable<T>对象(比如LINQ查询所返回的结果)
            //转换为一个IDictionary<Key,Value>对象。
            IDictionary<string, Game> stats = spartans.ToDictionary(key => key.Opponent);
            Console.WriteLine("Spartans vs. {0} {1}", stats["Notre Dame"].Opponent, stats["Notre Dame"].Score);
            Console.ReadLine();
        }
    }
}

LinQ转换运算符ToDictionary,布布扣,bubuko.com

时间: 2024-08-01 21:48:36

LinQ转换运算符ToDictionary的相关文章

LinQ转换运算符OfType&lt;T&gt;

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OfTypeDemo { class Program { static void Main(string[] args) { var numbers = new object[] { 1, "two", null, "3", 4 }; foreach (var anInt in

LinQ转换运算符ToLookup

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ToLookupDemo { class Program { //自定义类 public class Product { public string Code { get; set; } public string Description { get; set; } } static void Main(s

C++标准转换运算符dynamic_cast

dynamic_cast <new_type> (expression) dynamic_cast运算符,应该算是四个里面最特殊的一个,因为它涉及到编译器的属性设置,而且牵扯到的面向对象的多态性跟程序运行时的状态也有关系,所以不能完全的使用传统的转换方式来替代.但是也因此它是最常用,最不可缺少的一个运算符. 与static_cast一样,dynamic_cast的转换也需要目标类型和源对象有一定的关系:继承关系. 更准确的说,dynamic_cast是用来检查两者是否有继承关系.因此该运算符实

C++标准转换运算符const_cast

文章出处:http://www.cnblogs.com/ider/archive/2011/07/22/cpp_cast_operator_part2.html 前面讲了C++继承并扩展C语言的传统类型转换方式,最后留下了一些关于指针和引用上的转换问题,没有做详细地讲述.C++相比于C是一门面向对象的语言,面向对象最大的特点之一就是具有“多态性(Polymorphism)”. 要想很好的使用多态性,就免不了要使用指针和引用,也免不了会碰到转换的问题,所以在这一篇,就把导师讲的以及在网上反复查阅了

C++标准转换运算符reinterpret_cast

文章出处:http://www.cnblogs.com/ider/archive/2011/07/30/cpp_cast_operator_part3.html reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关类型之间的转换:它会产生一个新的值,这个值会有与原始参数(expressoin)有完全相同的比特位. 什么是无关类型?我没有弄清楚,没有找到好的文档来说明类型之间到底都有些什么关系(除了类的继承以外).

转换运算符

转换运算符: implict 与explicit 两个关键词. explicit为显示转换: struct Digit { byte value; public Digit(byte value) //constructor { if (value > 9) { throw new System.ArgumentException(); } this.value = value; } public static explicit operator Digit(byte b) // explici

C++学习笔记33 转换运算符

有时候我们想将一个类类型转换为另一个类类型,同时,这两个类并不存在继承关系,这时候我们就需要一种叫做转换运算符的运算符. 一个简单的例子.要将类A转换为int类型 #include <iostream> #include <string> using namespace std; class A{ private: int n; string str; public: A(int m,string s):n(m),str(s){ } }; int main(){ A a(10,&q

【转】C++标准转换运算符reinterpret_cast

原文链接:http://www.cnblogs.com/ider/archive/2011/07/30/cpp_cast_operator_part3.html reinterpret_cast <new_type> (expression) reinterpret_cast运算符是用来处理无关类型之间的转换:它会产生一个新的值,这个值会有与原始参数(expressoin)有完全相同的比特位. 什么是无关类型?我没有弄清楚,没有找到好的文档来说明类型之间到底都有些什么关系(除了类的继承以外).

C++标准转换运算符

参考 : [1]. Type conversions.  http://www.cplusplus.com/doc/tutorial/typecasting/ [2]. C++标准转换运算符. http://www.cnblogs.com/ider/archive/2011/07/22/cpp_cast_operator_part2.html