1

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"}

5
  • Use an Left Outer Join. See msdn : code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b Commented Apr 11, 2017 at 12:00
  • 1
    @jdweng: I see no need a join here... Commented Apr 11, 2017 at 12:01
  • userEducation.Where(e => ignoreEducationKeywords.All(i => !e.Contains(i))) Commented Apr 11, 2017 at 12:02
  • This code example smells like a homework Commented Apr 11, 2017 at 12:04
  • no, this is a task to parse resume from Linkedin PDF Commented Apr 11, 2017 at 12:13

4 Answers 4

8

That's a relatively straightforward query that can be constructed with Any or All, depending on your preferences:

var res = userEducation
    .Where(s => !ingoreEducationKeywords.Any(ignored => s.Contains(ignored)))
    .ToList();

or

var res = userEducation
    .Where(s => ingoreEducationKeywords.All(ignored => !s.Contains(ignored)))
    .ToList();

If the lists are very large, you could improve performance by using regex to match all words simultaneously:

var regex = new Regex(
    string.Join("|", ingoreEducationKeywords.Select(Regex.Escape))
);
var res = userEducation.Where(s => !regex.IsMatch(s)).ToList();

Demo.

Sign up to request clarification or add additional context in comments.

Comments

6

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)));

Comments

3

You can use Where, Any and Contains:

var list = userEducation.Where(ed => !ingoreEducationKeywords.Any(ik => ed.Contains(ik)));

It searches all occurences in userEducation where the education does not have any match in ingoreEducationKeywords.

2 Comments

Like Jon you're missing a trailing )
Like Jon I can't count @juharr :) Thanks
0
List<string> ingoreEducationKeywords = new List<string>() { "Uni", "School", "College", };
List<string> userEducation = new List<string>() { "MCS", "BCS", "School of Arts", "College of Medicine" };

var result = userEducation.Where(r => !ingoreEducationKeywords.Any(t => r.Contains(t))).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.