For example, I have two arrays:
var list1 = string[] {"1", "2", "3", "4", "5", "6"};
var list2 = string[] {"2", "3", "4"};
What I’m trying to do is –
- Get common items from
list1andlist2(eg. {“2”, “3”, “4”}) - Get different items
list1andlist2(eg. {“1”, “5”, “6”})
So I’ve tried with LINQ and –
var listDiff = list1.Except(list2); //This gets the desire result for different items
But,
var listCommon = list1.Intersect(list2); //This doesn't give me desire result. Comes out as {"1", "5", "6", "2", "3", "4"};
Any ideas?
Somehow you have got that result from somewhere else. (Perhaps you are writing out the contents of
listDIfffirst, and thought that it was fromlistCommon.) TheIntersectmethod does give you the items that exists in both lists:Output: