您的位置:首页 > 编程语言 > C#

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

2015-01-22 16:10 253 查看
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没有输出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: