简体   繁体   中英

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

可以使用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. this is the linq'ed answer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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