How can I compare a string to a "filter" list in linq?
c#, linq
Solution
You can use `Any` + `Contains`:
var items = foo.Where(s => !filter.Any(w => s.Contains(w)));
if you want to compare case-insensitively:
var items = foo.Where(s => !filter.Any(w => s.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0));
Update: If you want to exlude sentences where at least one word is in the filter-list you can use `String.Split()` and `Enumerable.Intersect`:
var items = foo.Where(sentence => !sentence.Split().Intersect(filter).Any());
`Enumerable.Intersect` is very efficient since it uses a `Set` under the hood. it's more efficient to put the long sequence first. Due to Linq's deferred execution is stops on the first matching word.
( note that the "empty" `Split` includes other white-space characters like tab or newline )
Problem
I'm trying to filter a collection of strings by a "filter" list... a list of bad words. The string contains a word from the list I dont want it. I've gotten so far, the bad Word here is "frakk": ``` string[] filter = { "bad", "words", "frakk" }; string[] foo = { "this is a lol string that is allowed", "this is another lol frakk string that is not allowed!" }; var items = from item in foo where (item.IndexOf( (from f in filter select f).ToString() ) == 0) select item; ``` But this aint working, why?