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();
        }
    }
}

 

相关文章:

  • 2021-05-29
  • 2021-08-11
  • 2022-12-23
  • 2021-11-12
  • 2021-05-31
  • 2022-12-23
  • 2021-09-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
相关资源
相似解决方案