Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

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 using LINQ to find all those that do not end with any of the disregards items?

like image 363
p.campbell Avatar asked Nov 01 '25 08:11

p.campbell


1 Answers

var nonJedi = candidates.Where(c => !disregard.Any(d => c.EndsWith(d)));
like image 185
LukeH Avatar answered Nov 02 '25 21:11

LukeH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!