C#中实现对列表(List)中的数据查重操作

更加详细的开发内容请参考: 微软官方Linq开发文档

一、列表查重操作核心如下

1.1、常用列表的查重操作核心如下:

  //查找出列表中的所有重复元素
        private static List<string> QueryRepeatElementOfList(List<string> list)
        {
            List<string> listTmp = new List<string>();
            if (list!=null && list.Count>0)
            {
                listTmp = list.GroupBy(x => x)
                              .Where(g => g.Count() > 1)
                              .Select(y => y.Key)
                              .ToList();
            }
            return listTmp;
        }

    
        //查找出列表中的所有重复元素及其重复次数
        private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
        {
            Dictionary<string,int> DicTmp = new Dictionary<string, int>();
            if (list != null && list.Count > 0)
            {
                DicTmp = list.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .ToDictionary(x => x.Key, y => y.Count());
            }
            return DicTmp;
        }

1.2、类列表的查重数据操作核心如下

 //静态扩展类(实现对类重复内容的操作)
    public static class Extention
    {
        public static IEnumerable<T> GetMoreThanOnceRepeatInfo<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
        { //返回第二个以后面的重复的元素集合
            return extList
                .GroupBy(groupProps)
                .SelectMany(z => z.Skip(1)); //跳过第一个重复的元素
        }
        public static IEnumerable<T> GetAllRepeatInfo<T>(this IEnumerable<T> extList, Func<T, object> groupProps) where T : class
        {
            //返回所有重复的元素集合
            return extList
                .GroupBy(groupProps)
                .Where(z => z.Count() > 1) 
                .SelectMany(z => z);
        }
    }

二、使用方法


 //货物信息
    class GoodsInfo
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }

        public GoodsInfo()
        {
                
        }

        public GoodsInfo(string code,string name,string position)
        {
            this.Code = code;
            this.Name = name;
            this.Position = position;
        }
    }

  class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("------------------列表操作-------------------------");
            //初始化列表
            List<string> listTmp = InitList();

            //查找出列表中的所有重复元素
            List<string> repeatList = QueryRepeatElementOfList(listTmp);
            if (repeatList!=null && repeatList.Count>0)
            {
                Console.WriteLine("---当前所有的重复元素为:---");
                foreach (var item in repeatList)
                {
                    Console.WriteLine(item);
                }
            }

            //查找出列表中的所有重复元素
            Dictionary<string, int> repeatEleAndCountDic = QueryRepeatElementAndCountOfList(listTmp);
            if (repeatEleAndCountDic != null && repeatEleAndCountDic.Count > 0)
            {
                Console.WriteLine("---当前所有的重复元素个数为:---");
                foreach (var item in repeatEleAndCountDic)
                {
                    Console.WriteLine("重复内容为:"+item.Key+ " 重复次数为:"+item.Value);
                }
            }

            List<int> IntTmp = new List<int> { 10, 20, 30, 20, 50,10 };
            int Sum = IntTmp.Sum();
            Console.WriteLine("---获取列表的和:{0}",Sum);
            double Average = IntTmp.Average();
            Console.WriteLine("---获取列表的平均数:{0}", Average);
            int Max = IntTmp.Max();
            Console.WriteLine("---获取列表的最大值:{0}", Max);
            int Min = IntTmp.Min();
            Console.WriteLine("---获取列表的最小值:{0}", Min);
            List<int> DistinctInfo = IntTmp.Distinct().ToList();
            foreach (var item in DistinctInfo)
            {
                Console.WriteLine("---获取列表的不重复数据:{0}", item);
            }
           

            Console.WriteLine();
            Console.WriteLine("-------------------类列表操作---------------------------");
            //初始化类列表
            List<GoodsInfo> goodsInfos = InitGoodsInfoList();

            //查询到列表中的所有重复数据
            List<GoodsInfo> AllRepeatGoodsInfos = QueryAllRepeatInfoOfList(goodsInfos);
            if (AllRepeatGoodsInfos!=null && AllRepeatGoodsInfos.Count>0)
            {
                Console.WriteLine("---当前【满足(名称与位置相同)条件】所有的重复的货物信息为:---");
                foreach (var item in AllRepeatGoodsInfos)
                {
                    Console.WriteLine("编号:{0},名称:{1},位置:{2}",item.Code,item.Name,item.Position);
                }
            }

            //查询到列表中重复一次以上的数据
            List<GoodsInfo> RepeatMoreThanOnceGoodsInfos = QueryMoreThanOnceInfoList(goodsInfos);
            if (RepeatMoreThanOnceGoodsInfos != null && RepeatMoreThanOnceGoodsInfos.Count > 0)
            {
                Console.WriteLine("---当前【满足(名称相同)条件】所有的重复一次以上的货物信息为:---");
                foreach (var item in RepeatMoreThanOnceGoodsInfos)
                {
                    Console.WriteLine("编号:{0},名称:{1},位置:{2}", item.Code, item.Name, item.Position);
                }
            }

            Console.ReadLine();
        }


        #region   列表操作

        //初始化一个列表
        private static List<string> InitList()
        {
            List<string> listTmp = new List<string>();
            string str = "101";
            for (int i = 0; i < 3000; i++)
            {
                string strTmp = str + i;
                switch (strTmp)
                {
                    case "10112":
                    case "10118":
                    case "10124":
                        strTmp = "10107";
                        break;
                    case "10126":
                    case "10137":
                    case "10138":
                    case "10149":
                        strTmp = "10111";
                        break;
                    default:
                        break;
                }
                listTmp.Add(strTmp);

            }

            return listTmp;
        }



        //查找出列表中的所有重复元素
        private static List<string> QueryRepeatElementOfList(List<string> list)
        {
            List<string> listTmp = new List<string>();
            if (list!=null && list.Count>0)
            {
                listTmp = list.GroupBy(x => x)
                              .Where(g => g.Count() > 1)
                              .Select(y => y.Key)
                              .ToList();
            }
            return listTmp;
        }

    
        //查找出列表中的所有重复元素及其重复次数
        private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
        {
            Dictionary<string,int> DicTmp = new Dictionary<string, int>();
            if (list != null && list.Count > 0)
            {
                DicTmp = list.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .ToDictionary(x => x.Key, y => y.Count());
            }
            return DicTmp;
        }

        #endregion


        #region   类操作

        //初始化类列表
        private static List<GoodsInfo> InitGoodsInfoList()
        {
            List<GoodsInfo> goodsInfos = new List<GoodsInfo>()
            {
                new GoodsInfo("10101","海尔冰箱","0102"),
                new GoodsInfo("10102","海尔电视","0103"),
                new GoodsInfo("10103","海尔空调","0104"),
                new GoodsInfo("10104","海尔净化器","0105"),
                new GoodsInfo("10105","海尔冷风机","0106"),

            };

            GoodsInfo goodsInfo1 = new GoodsInfo("10106", "海尔冰箱", "0102");
            GoodsInfo goodsInfo2 = new GoodsInfo();
            goodsInfo2.Code = "10108";
            goodsInfo2.Name = "海尔净化器";
            goodsInfo2.Position = "0108";

            goodsInfos.Add(goodsInfo1);
            goodsInfos.Add(goodsInfo2);

            return goodsInfos;

        }

        //查询到列表中的所有重复数据(条件为:名称与位置相同)
        private static List<GoodsInfo> QueryAllRepeatInfoOfList(List<GoodsInfo> goodsInfos)
        {
            List<GoodsInfo> goodsInfoTmpList = new List<GoodsInfo>();

            if (goodsInfos!=null && goodsInfos.Count>0)
            {
                goodsInfoTmpList=goodsInfos.GetAllRepeatInfo(x => new { x.Name, x.Position })
                                           .ToList();
                         
            }
            return goodsInfoTmpList;
        }

        //查询到重复了一次以上的数据
        private static List<GoodsInfo> QueryMoreThanOnceInfoList(List<GoodsInfo> goodsInfos)
        {
            List<GoodsInfo> goodsInfoTmpList = new List<GoodsInfo>();

            if (goodsInfos != null && goodsInfos.Count > 0)
            {
                goodsInfoTmpList = goodsInfos.GetMoreThanOnceRepeatInfo(x => new { x.Name})
                                           .ToList();

            }
            return goodsInfoTmpList;
        }

        #endregion


    }//Class_end

三、结果如下:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛奶咖啡13

我们一起来让这个世界有趣一点…

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值