zoukankan      html  css  js  c++  java
  • 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(string[] args)
            {
                List<Product> products = new List<Product>
                {
                    new Product{Code="SPM1",Description="1"},
                    new Product{Code="LME1",Description="3"},
                    new Product{Code="SUP1",Description="4"},
                    new Product{Code="SPM2",Description="2"},
                    new Product{Code="SUP2",Description="5"}
                };
                //转换为Lookup类型,得到的结果已经分组了
                Lookup<string, string> lookup = (Lookup<string, string>)
                    products.ToLookup(c => c.Code.Substring(0, 3), c => c.Code + " " + c.Description);
                //在结果中循环
                foreach (IGrouping<string, string> group in lookup)
                {
                    Console.WriteLine(group.Key);
                    foreach (string s in group)
                        Console.WriteLine("   {0}", s);
                }
                Console.ReadLine();
                //取得其中一组值
                IEnumerable<string> spmGroup = lookup["SPM"];
                foreach (var str in spmGroup)
                    Console.WriteLine(str);
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    20170519
    20170515
    20170511
    20170509
    20170505
    使用 FirewallD 构建动态防火墙
    Elasticsearch 5.2.1Cluster 搭建
    elk,centos7,filebeat,elasticsearch-head详细安装步骤
    elk,centos7,filebeat,elasticsearch-head集成搭建
    memcached
  • 原文地址:https://www.cnblogs.com/swtool/p/3840291.html
Copyright © 2011-2022 走看看