C# Compare one List string with substring of other list string

I have two lists

List<string> ingoreEducationKeywords= new List<string>(){"Uni", "School", "College",}; 
List<string> userEducation= new List<string>(){"MCS", "BCS", "School of Arts","College of Medicine"}; 

Now I want to get a list which has no substring from the ignore list.

require list {"MCS", "BCS"}

Jon Skeet
people
quotationmark

It's a matter of phrasing what you want in a way that leads to a natural translation into LINQ:

  • You want items from userEducation (that suggests you'll start with userEducation)
  • Where none of ignoreEducationKeywords are substrings.
    • "None" is equivalent to "not any"
    • To check for substrings you can use Contains

That leads to:

var query = userEducation
   .Where(candidate => !ignoredKeyWords.Any(ignore => candidate.Contains(ignore)));

The same thought process can help in many other queries.

Another option would be to create your own None extension method, assuming you're using LINQ to Objects:

public static class Extensions
{
    public static bool None(this IEnumerable<T> source, Func<T, bool> predicate)
        => !source.Any(predicate);
}

Then you could rewrite the query without the negation:

var query = userEducation
   .Where(candidate => ignoredKeyWords.None(ignore => candidate.Contains(ignore)));

people

See more on this question at Stackoverflow