Linq Any() vs foreach
c#
Solution
It's almost same code, the only difference being that with the second code snippet you're gonna get a `NullReferenceException` at runtime if the `name` variable is `null` because you will be calling the `.ToUpper()` method on a null instance. The first looks shorter, safer and more readable, it's what I would use. And to ensure that there won't be any NREs:
return user
.Permissions
.Any(x => string.Equals(x.UpperName, name, StringComparison.OrdinalIgnoreCase));
Problem
Just wonder which approach is faster and better to use or which do you prefer ``` bool userHavePermission = user.Permissions.Any(x => x.UpperName == "ADMINISTRATOR"); ``` or ``` foreach (Permission p in _Permissions) { if (p.UpperName == name.ToUpper()) return true; } return false; ``` Thanks