tags:

views:

104

answers:

3

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

+4  A: 

It can be done using Except() and Concat() LINQ methods:

LISTONE.Except(LISTTWO).Concat(LISTTWO.Except(LISTONE))
Elisha
@Elisha: this looks promising. this will include "B" which isn't in OP's `listtemp`, but that's possibly a typo on OP's part...
David Hedlund
+2  A: 
LISTONE.Except(LISTTWO).Union(LISTTWO.Except(LISTONE)) //.Distinct()?

EDIT: The Union method implements Distinct behaviour, so Distinct would be redundant.

spender
@spender: can the result from that query ever be non-distinct?
David Hedlund
To be honest, I'm not sure. Thinking about it a little more, I suppose Union is actually an implementation of UNION (SQL) behaviour as opposed to Concat which is equivalent to UNION ALL, so the Distinct is most likely superfluous.
spender
@spender: regardless of how the union works, if you concatinate two lists containing only the items that are not in the other list, it seems that you could logically never end up with duplicates with `UNION` *or* `UNION ALL`
David Hedlund
Ah, but supposing one of the lists contains two identical items that don't occur in the second? I guess the OP should tighten up their question.
spender
+1  A: 

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))
Jamiec