How to use LINQ to query a list of strings that do not contain substring entries from another list

string[] candidates = new string[] {
    "Luke_jedi", "Force_unknown", 
    "Vader_jedi" , "Emperor_human", "r2d2_robot"
};

string[] disregard = new string[] {"_robot", "_jedi"};

//find those that aren't jedi or robots.
var nonJedi = candidates.Where(c=>
              c.??? //likely using EndsWith() and Any()
              ); 

How would you implement this solution with LINQ to find all those that do not end with any of the neglect elements?

+3
source share
2 answers
var nonJedi = candidates.Where(c => !disregard.Any(d => c.EndsWith(d)));
+7
source

Source: https://habr.com/ru/post/1745891/


All Articles