码迷,mamicode.com
首页 > Windows程序 > 详细

C# 两个List泛型用 Linq去重复数据 或者 得到重复数据

时间:2015-01-22 17:23:04      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

1, 两个List泛型用Linq去重复数据

Code:

            List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
            List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };


            List<string> newList = lstA.FindAll(x => !lstB.Contains(x));
            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

 

控制台运行结果

技术分享

图1

 

图1 为什么会出现这个情况,什么都没有输出。下面修改下程序,请看2

 

2,先看 Code:

            List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
            List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };


            List<string> newList = lstB.FindAll(x => !lstA.Contains(x));
            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

再看结果:

技术分享

图2

 看下加粗线 的代码 和图2结果,想必你看出所以然了。

把 A数组 当做 A区间 B数组 当做 B区间, B区间的 范围大于 A区间 ,把A、B两区间的 公共部分除掉,不同部分找出来。

1 结果 没有数据输出,原因就在此。

 3,在 A数组 在 加个 mac地址 这时候 A、B两区间范围一样大

code:

            List<string> lstA = new List<string> { "E00401501B652562", "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
            List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };


            List<string> newList = lstB.FindAll(x => !lstA.Contains(x));
            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

再看结果:

技术分享

图3

 

图3和图2的结果一样,不重复的数据 应该是 E00401501B652562,E00401501B652569这两个mac地址才正确。

这时候代码怎么调整呢?看 4

 

4,Code

            List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652562", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
            List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };
            List<string> listAll = new List<string>();
            List<string> listResult = new List<string>();

            var listUnionAll = lstA.Union(lstB).OrderBy(t => t);//排序合并数据
            
            foreach (var item in listUnionAll)
            {
                listAll.Add(item);
            }

            List<string> newList = listAll.FindAll(x => !lstA.Contains(x));//去重复,组合新的List集合
            List<string> newList2 = listAll.FindAll(x => !lstB.Contains(x));

            var Unionlist = newList.Union(newList2).OrderBy(t => t);//排序合并数据

            foreach (var item in Unionlist)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

 

结果

技术分享

 

5,找重复数据 把linq不等于号去掉就行了。

            List<string> lstA = new List<string> { "E00401501B652562", "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
            List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };


            List<string> newList = lstB.FindAll(x => lstA.Contains(x));
            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

结果

技术分享

mac地址 62和69没有输出。

C# 两个List泛型用 Linq去重复数据 或者 得到重复数据

标签:

原文地址:http://www.cnblogs.com/suntanyong88/p/4241790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!