使用Linq查找重复

时间:2023-03-10 06:45:04
使用Linq查找重复
 namespace RemoveTheSame
{
class Program
{
static void Main(string[] args)
{
List<User> list = new List<User>()
{
new User{Id=,Name="user1",Pwd=""},
new User{Id=,Name="user1",Pwd=""},
new User{Id=,Name="user2",Pwd=""}
};
GetTheSame(list, out string tkey);
Console.WriteLine($"The Same is {tkey}");
Console.ReadKey();
}
public static void GetTheSame(List<User> listOld, out string tkey/*,out User user*/)
{
tkey = null;
var result = from x in listOld
group x by x.Name into g
where g.Count() >
select g;
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; } }
} 其实就是一行代码解决的问题~
list.GroupBy(x => x.Name).Where(x => x.Count() > ).ToList().ForEach(x => Console.WriteLine(x.Key));