• c# 敏捷2 ForEach ToDictionary ToLookup Except比较


     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Diagnostics;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace ConsoleApplication2
    10 {
    11    
    12     class Program
    13     {
    14         public  class post
    15         {
    16             public string id { get; set; }
    17             public string name { get; set; }
    18             public int age { get; set; }
    19             public string pd { get; set; }
    20         }
    21         static void Main(string[] args)
    22         {
    23             var product = new List<post>
    24           {
    25                 new post { name="123"},
    26                 new post { name="124"},
    27                 new post { name="125"},
    28                 new post { name="126"},
    29           };
    30             product.ToList().ForEach(a => a.id = Guid.NewGuid().ToString("N"));//进行遍历赋值
    31 
    32             var pp = product.ToDictionary(a => a.name, a => a.id); //转成字典
    33             Console.WriteLine(pp.FirstOrDefault().Key + " " + pp.FirstOrDefault().Value);
    34 
    35             product.Add(new post { name = "123", id = Guid.NewGuid().ToString("N") });
    36             var pd = product.ToLookup(a => a.name, a => a.id);//groupby之后转成字典,取不同项
    37             Console.WriteLine(pp.FirstOrDefault().Key + " " + pp.FirstOrDefault().Value);
    38 
    39             var pro = new List<post>
    40           {
    41                 new post { id = Guid.NewGuid().ToString("N"),name="123",age=1,pd="sad"},
    42                 new post { id = Guid.NewGuid().ToString("N"),name="124",age=11,pd="sad"},
    43                 new post { id = Guid.NewGuid().ToString("N"),name="125",age=111,pd="sad"},
    44                 new post {id = Guid.NewGuid().ToString("N"), name="126",age=1111,pd="sad"},
    45           };
    46             var prd = new List<post>
    47           {
    48                 new post { id = Guid.NewGuid().ToString("N"),name="123",age=1,pd="sad"},
    49                 new post { id = Guid.NewGuid().ToString("N"),name="124",age=11,pd="sad"},
    50                 new post {id = Guid.NewGuid().ToString("N"), name="1245",age=111,pd="sad"},
    51                 new post {id = Guid.NewGuid().ToString("N"), name="126",age=1111,pd="sad"},
    52           };
    53             var dsz = pro.Select(s => new { id = s.age, name = s.name, pd = s.pd });
    54             var dsx = prd.Select(s => new { id = s.age, name = s.name, pd = s.pd });
    55             var dsc = dsz.Except(dsx).Count();
    56             if (dsc > 0)
    57                 Console.WriteLine("修改过");
    58             else
    59                 Console.WriteLine("未修改");
    60 
    61             if (pro.Select(s => new { id = s.age, name = s.name, pd = s.pd }).Except(prd.Select(s => new { id = s.age, name = s.name, pd = s.pd })).Count() > 0)
    62                 Console.WriteLine("修改过");
    63             else
    64                 Console.WriteLine("未修改");
    65             Console.WriteLine(dsc);
    66             
    67             Console.Read();
    68 
    69         }
    70 
    71     }
    72 
    73 
    74 
    75 }
  • 相关阅读:
    Swift基础
    win32多线程 (六)信号量 Synchronization
    win32 多线程 (五)Event
    win32多线程 (四) Mutex
    win32多线程(三) 死锁
    win32多线程 (二)线程同步之临界区 (critical sections)
    win32多线程 (一) 线程创建与结束等待
    简单Factory模式
    单键模式
    jquery中click事件重复绑定问题
  • 原文地址:https://www.cnblogs.com/LiuFengH/p/10115054.html
Copyright © 2020-2023  润新知