Basic LINQ query to check if string occurs within a list

Someone asked on Stack Overflow:

I have a list MyGroups and a string ‘AllowedGroups’.

For example I have string in MyGroups.Name = “Admin Only” and AllowedGroups =“Admin Only, Normal Group”. I can’t understand why the following expression evaluates to false when it should be true:

model.MyGroups.Any(m => m.Name.Contains(AllowedGroups)

I posted the following answer, which was chosen as the accepted answer and received 6 upvotes:

I think what you need, based on the information you’ve provided, is the opposite of what you have.

Something like this:

var allowedGroupsArray = AllowedGroups.Split(',');
var result = model.MyGroups.Any(m => allowedGroupsArray.Contains(m.Name));

Specifically, using Array.Contains() inside the .Any() call should generate basically a WHERE ... IN type clause in your SQL.


Originally posted on Stack Overflow — 6 upvotes (accepted answer). Licensed under CC BY-SA.

signed letter b

Dad. Geek. Gamer. Software developer. Cloud user. Old Car enthusiast.  Blogger.


Top Posts


profile for Nate on Stack Exchange, a network of free, community-driven Q&A sites
a proud member of the blue team of 512KB club
Thoughts, opinions, and ideas shared here are my own. © 2026 Nate Bross.