Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ If .Any matches .Any

Tags:

c#

linq

I have 2 string arrays, and I would like to return if any of them exists in _authRole array. How is that done?

 string[] _userRoles = userdata.Split(',');

 string[] _authRoles = AuthRoles.Split(',');


 bool isAuthorized = _authRoles.Any(_userRoles ??);

/M

like image 334
Lasse Edsvik Avatar asked May 11 '10 14:05

Lasse Edsvik


1 Answers

If what you want is to determine if _authRoles and _userRoles have at least one common item, then use:

bool isAuthorized = _authRoles.Intersect(_userRoles).Any();

You can also query the result of Intersect in any other way you choose.

like image 173
Jon Avatar answered Nov 13 '22 10:11

Jon