Jynn
Jynn

Reputation: 287

Basic LINQ query to check if string occurs within a list

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)

Upvotes: 0

Views: 73

Answers (2)

Nate
Nate

Reputation: 30636

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.

Upvotes: 6

Med.Amine.Touil
Med.Amine.Touil

Reputation: 1235

it returns false because Admin Only does not contain Admin Only, Normal Group

Upvotes: 0

Related Questions