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();
            }
        }
    }
  • 相关阅读:
    Zju1876 Edit Step Ladders
    Candy糖果盒
    哈希
    最长上升子序列 nlogn
    电费结算
    【luoguP5656】二元一次不定方程(gcd,exgcd,裴蜀定理,不定方程初步)
    DP技巧
    高精
    【CSP烤前注意】
    [Jsoi2015] 种花
  • 原文地址:https://www.cnblogs.com/swtool/p/3840291.html
Copyright © 2011-2022 走看看