简体   繁体   English

如何使用LINQ在C#中获得两个列表的差异

[英]How to get the difference of two list in C# using LINQ

I've two lists like 我有两个清单,例如

List<String> LISTONE=new List<String>() 

and

List<String> LISTTWO=new List<String>() 

and which contains 并且其中包含

LISTONE   "A"
          "B"
          "C"
LISTTWO   "A"
          "D"
          "E"

and the required out put is 所需的输出是

LISTTEMP  "B"   
          "C"
          "D"
          "E"

is there any way to do this using LINQ 有什么办法可以使用LINQ做到这一点

可以使用Except()Concat() LINQ方法来完成:

LISTONE.Except(LISTTWO).Concat(LISTTWO.Except(LISTONE))
LISTONE.Except(LISTTWO).Union(LISTTWO.Except(LISTONE)) //.Distinct()?

编辑:联合方法实现不同的行为,所以不同将是多余的。

Assuming you want "All elements which do not appear in both lists", in which case, as my note above mentions, "B" should also be in there. 假设您要“所有元素都不会出现在两个列表中”,在这种情况下,正如我上面提到的,“ B”也应该在其中。 this is the linq'ed answer. 这是linq'ed的答案。

one.Concat(two).Except(one.Intersect(two))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM