• 集合-字典(Lookup/SortedDictionary)


    Lookup<TKey, TElement>非常类似于Dictionary<TKey, TValue>,但是把键映射在一个值集上。

    必须调用ToLookup方法创建Lookup<TKey, TElement>对象。Lookup<TKey, TElement>对象是不可变的,无法向对象添加,移除元素。

    public class User
        {
            public string Address;
            public string Name;
            public int Age; 
        }
        class Program
        {
            static void Main(string[] args)
            {
                List<User> users = new List<User> { new User{Address="bj",Name="rxm",Age=27},
                                                      new User{Address="bj",Name="cwr",Age=24},
                                                      new User{Address="sh",Name="zcl",Age=28},
                                                      new User{Address="zz",Name="hst",Age=29}};
    
                Lookup<char, string> needUsers = (Lookup<char, string>)users.ToLookup(p => Convert.ToChar(p.Address.Substring(0, 1)),
                                                                                  p => p.Name + "-" + p.Age.ToString());
    
    
                foreach (IGrouping<char, string> item in needUsers)
                {
                    Console.WriteLine(item.Key); //b s z
                    foreach (string str in item)
                    {
                        Console.WriteLine("   {0}", str);
                    }
                }
                //-----
                Console.WriteLine("--------------");
                IEnumerable<string> results = needUsers['b'];
                foreach (var item in results)
                {
                    Console.WriteLine(item);
                }
                Console.Read();
            }
        }

    SortedDictionary<Tkey,Tvalue>中的键都必须唯一。与这个Dictionary<Tkey,Tvalue>类不同的是,它排了序。

     SortedDictionary<string, string> items = new SortedDictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);//忽略大小写
                items.Add("masanli", "逗你玩");
                items.Add("maji", "宇宙牌香烟");
                items.Add("liubaorui", "连升三级");
                items.Add("houbaolin", "北京话");
                foreach (KeyValuePair<string, string> item in items)
                {
                    Console.WriteLine(string.Format("key:{0},value:{1}", item.Key, item.Value));
                }
  • 相关阅读:
    假脱机技术
    HTTP报文
    字符串转换成浮点数的方法
    表变量与临时表空间
    规范浮点数
    什么是批处理
    浅谈性能测试、压力测试和负载测试
    关于CSDN的一个安全漏洞
    HttpWatch7.0测试工具
    vbscript能做什么
  • 原文地址:https://www.cnblogs.com/hometown/p/3223366.html
Copyright © 2020-2023  润新知