使用Linq查找重复

使用Linq查找重复

 
复制代码
 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 }
复制代码

 

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)
        {
            List<User> list = new List<User>()
            {
                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<User> 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<string,User>  类型
            //g里面有多对       string  List<User>
            //string 为 key     每个 string 又继而对应一个集合  集合元素类型为User

            //IGrouping<string,>  //表示具有公共键的对象的集合。
            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; }

    }
}

 

转载于:https://www.cnblogs.com/cjm123/p/9922740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值