欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Linq查找重复

程序员文章站 2022-11-17 09:19:52
1 namespace RemoveTheSame 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 List list = new List() 8 { 9 new Use... ......
 1 namespace removethesame
 2 {
 3     class program
 4     {
 5         static void main(string[] args)
 6         {
 7             list<user> list = new list<user>()
 8             {
 9                 new user{id=1,name="user1",pwd="123"},
10                 new user{id=2,name="user1",pwd="123"},
11                 new user{id=3,name="user2",pwd="123"}
12             };
13             getthesame(list, out string tkey);
14             console.writeline($"the same is {tkey}");
15             console.readkey();
16         }
17         public static void getthesame(list<user> listold, out string tkey/*,out user user*/)
18         {
19             tkey = null;
20             var result = from x in listold
21                          group x by x.name into g
22                          where g.count() > 1
23                          select g;
24             foreach (var item in result)
25             {
26                 tkey = item.key;
27             }
28         }
29     }
30     public class user
31     {
32         public string name { get; set; }
33         public int id { get; set; }
34         public string pwd { get; set; }
35 
36     }
37 }