Compare two Arrays/Lists, return elements Not in common

Hi, I have two Arrays/Lists:

List/Array 1: A,B,C,D
List/Array 2: A,B,E

I want to retrieve the elements which are Not in common. One expression for those not in common and contained in 1 and another for those in 2

so. if main collection is 1, result is C,D
if main collection is 2, result is E

Edit: Comparison is case insensitive, trim whitespace

How do I do this?

Hi,

Can you try the following expression?

if main collection is 1, result is C,D

list1.Where(Function(x) not list2.Contains(x)).toList()

if main collection is 2, result is E

list2.Where(Function(x) not list1.Contains(x)).toList()

Regards,

Hi @Yoichi, how do you modify this to be case insensitive and ignore whitespace?

Hi,

how do you modify this to be case insensitive and ignore whitespace?

How about the following?

list1.Where(Function(x) not list2.Select(Function(y) y.Trim().ToLower()).Contains(x.Trim().ToLower())).toList()

Regards,

@Yoichi
Thanks, How do you efficiently convert all the elements in the List above back to Uppercase?

Hi,

The above expression converts to lower case only for evaluation. So, result list has original string.
If you want to convert all the items of the list to upper case, the following will work.

list3.Select(function(x) x.ToUpper()).toList()

Regards,

@Yoichi Hi, I used this code

list1.Where(Function(x) not list2.Select(Function(y) y.Trim().ToLower()).Contains(x.Trim().ToLower())).toList()

and I ended up with list items with blank content (e.g. “”, " " etc…) how do I remove these?

Hi,

How about the following?

list1.Where(Function(x) not list2.Select(Function(y) y.Trim().ToLower()).Contains(x.Trim().ToLower())  AndAlso Not String.IsNullOrWhiteSpace(x)).toList()

Regards,