tags:

views:

75

answers:

2

Given this LINQ expression:

items.Select( i => i.ToLowerInvariant() ).Except( keywords )

Is there a way to express that where you preserve the casing of the input, without using a Where()?

The Where approach:

items.Where( i => !keywords.Contains( i.ToLowerInvariant() ) )

I like the way the Except approach reads, but I don't want the altered output.

Any ideas?

+3  A: 

There is an overload to Except() which takes an IEqualityComparer - you can use on of the built-in string comparers:

items.Except( keywords, StringComparer.InvariantCultureIgnoreCase );
LBushkin
You can use the built-in StringComparer.InvariantCultureIgnoreCase.
Julien Lebosquain
Good point ... I'm updating my example.
LBushkin
+1  A: 

You can use the overload of Except that takes an IEqualityComparer<T>. In this case you don't need to roll your own comparer: the built-in StringComparer.InvariantCultureIgnoreCase class does exactly what you need:

items.Except(keywords, StringComparer.InvariantCultureIgnoreCase);
LukeH