Joe Smith
Joe Smith

Reputation: 139

get strings that are in List<String> excluding strings in List<String[]>

How would I go about getting all the strings that appear in List<string> except for the strings that appear in List<string[]>. I can get this to work if they are both List<string> by doing

IEnumerable<string> list3 = List1.Except(List2);

But I cannot figure out how to do it using List<string[0]> in place of list2

Upvotes: 1

Views: 953

Answers (1)

Jeff Yates
Jeff Yates

Reputation: 62387

You should use SelectMany to flatten List<string[]> into a single IEnumerable<string>. Assuming list2 is of type List<string[]>, you can do:

var list3 = list1.Except(list2.SelectMany(x=>x));

However, if you just want the first string[] in List<string[]> then, assuming that there is at least one entry in the list, you could do:

var list3 = list1.Except(list2.First());

Additional
list2.SelectMany(x=>x) is equivalent to:

var flattenedList2 = from x in list2
                     from y in x
                     select x;

Upvotes: 6

Related Questions