❔ Exclude items from linq query if they contain one from an array of substrings

I've got a list of strings, that if they appear anywhere in a linq query column i want to exclude the row. I can find loads of examples of exact matches, or where the column value appears in a list, but not the other way around, and I can't figure it out
10 Replies
superbobface
superbobfaceOP2y ago
var providers = from st in context.Providers where st.Practice == dbPractice.FirstOrDefault() && st.Active && !exclusions.Contains(st.Name) select st; this is the reverse, looking to see if st.Name appears in any of the strings in exclusions (List<string>) but i want it the other way around
Angius
Angius2y ago
What do you mean "the other way around"? Whether a list of exclusions is contained in the name?
superbobface
superbobfaceOP2y ago
yea exactly that and then i'll drop those out
Angius
Angius2y ago
Uh, I can already say that "Thomas" will not contain [ "Bob", "Catherine", "Thomas" ] no matter what you do The list can contain the name The name can't really contain a list
superbobface
superbobfaceOP2y ago
quite if the exlucsion list was ["a","e"] then Thomas would be kicked out but Bob wouldn't basically - I probably can't do it in linq? I tried a function whic returns bool, but linq to sql doens't like that of course
Angius
Angius2y ago
var providers = await _context.Providers
.Where(p => !exclusions.Any(e => s.Name.Contains(e)))
// ...
.ToListAsync();
var providers = await _context.Providers
.Where(p => !exclusions.Any(e => s.Name.Contains(e)))
// ...
.ToListAsync();
Something like this
superbobface
superbobfaceOP2y ago
ah okay, I did see an exmaple similar, let me try that
Angius
Angius2y ago
As a side note, any reason to use Lin12Sql instead of Entity Framework Core?
superbobface
superbobfaceOP2y ago
sorry this is EFCore, apologies not linq to sql oh no i'm confused thank you tho
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server