1 namespace RemoveTheSame 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Listlist = new List () 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 listOld, out string tkey/*,out User user*/)18 {19 tkey = null;20 var result = from x in listOld21 group x by x.Name into g22 where g.Count() > 123 select g;24 foreach (var item in result)25 {26 tkey = item.Key;27 }28 }29 }30 public class User31 {32 public string Name { get; set; }33 public int Id { get; set; }34 public string Pwd { get; set; }35 36 }37 }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 使用Linq查找重复{ class Program { static void Main(string[] args) { Listlist = new List () { new User{Id=1,Name="user1",Pwd="123"}, new User{Id=2,Name="user1",Pwd="123"}, new User{Id=3,Name="user2",Pwd="123"} }; string tkey = ""; GetTheSame(list, out tkey); //Console.WriteLine($"The Same is {tkey}"); Console.ReadKey(); } public static void GetTheSame(List listOld, out string tkey/*,out User user*/) { tkey = null; var resulst = from x in listOld group x by x.Name into g select g; var result = from x in listOld group x by x.Name into g //根据 name 分组 返回 一个集合(集合中每个元素 又是一个集合) where g.Count() > 1 // 集合中每个 元素集合行数大于1的 select g; // g为 IGrouping 类型 //g里面有多对 string List //string 为 key 每个 string 又继而对应一个集合 集合元素类型为User //IGrouping //表示具有公共键的对象的集合。 foreach (var item in result) { tkey = item.Key; } } } public class User { public string Name { get; set; } public int Id { get; set; } public string Pwd { get; set; } }}