c# 敏捷2 ForEach ToDictionary ToLookup Except比較

weixin_30588675發表於2020-04-05
 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 }

 

轉載於:https://www.cnblogs.com/LiuFengH/p/10115054.html

相關文章