比较两个列表并返回不同的值和差异 [英] Comparing two Lists and returning the distinct values and the differences

查看:141
本文介绍了比较两个列表并返回不同的值和差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

 列表A {A,B,C,D} 

List B {A,E,F,G}



我需要产生三个列表: p>

只有在列表A中的项目



(B,C,D)



只有列表B中的项目



(E,F,G)



一个与



(A)中的项目



由于这些列表实际上是注册表项,可能有大量的它们,所以我可以预见巨大的性能开销,如果我选择使用传统的ForEach或For(int i ...)方法。



我不反对这些,如果他们会有效地工作,但我更喜欢使用Linq。



有人有任何想法吗?



我不在乎相同的记录。



一个 IEquatable<> 类,将比较元素,但它是如何使用这创建我所困扰的所需的输出。



提前感谢。



顺便说一句,我使用VS2012与.NET 4.5

解决方案

  var A = new List< string>(){A,B,C,D}; 
var B = new List< string>(){A,E,F,G};

A.Except(B).ToList()
//输出List< string>(2){B,C,D}
B .Except(A).ToList()
// outputs List< string>(2){E,F,G}
B.Intersect
// outputs List< string>(2){A}


I have two lists:

List A {A, B, C, D}

List B {A, E, F, G}

I need to produce three lists:

One with the items only in list A

(B, C, D)

One with the items only in list B

(E, F, G)

One with the items in both

(A)

Given that the lists are actually registry keys, there could be a huge number of them so I can foresee a huge performance overhead if I choose to use traditional ForEach or For(int i...) methods.

I am not averse to these if they will do the job efficiently but I would prefer to use Linq.

Has anyone got any ideas?

I don't care about identical records.

I have already created an IEquatable<> class that will compare the elements, but it is how to use this to create my required outputs that I am struggling with.

Thanks in advance.

By the way I am using VS2012 with .NET 4.5

解决方案

var A = new List<string>() { "A", "B", "C", "D" };
var B = new List<string>() { "A", "E", "F", "G" };

A.Except(B).ToList()
// outputs List<string>(2) { "B", "C", "D" }
B.Except(A).ToList()
// outputs List<string>(2) { "E", "F", "G" }
B.Intersect(A).ToList()
// outputs List<string>(2) { "A" }

这篇关于比较两个列表并返回不同的值和差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆