Q: LINQ: How do I find all the objects whose Array property contains a particular value using LINQ I've been searching for a while and could not find exactly what I need.
This questioning might be devoted, since, I suspect there are some alike questions here already. But I think I have a specific case.
I have an object User that contains the property as Array of permissions.
I'm looking for the correct way to grab all the users with a particular permission.
I tried the following:
List<User> users = UserRepository.GetUser().Where(u => u.Permissions[0].Equals("MyPermission")).ToList();

When doing this, I'm getting an error: 

"Index was outside the bounds of the array."

What is the correct way of doing this?

A: Using .Any() is a safe way
List<User> users = UserRepository.GetUser()
    .Where(u => u.Permissions.Any(x => x.Equals("MyPermission"))
    .ToList();


A: You get an exception because you blindly index Permissions with [0], without checking that it has any values at all.
Adding a check for length would fix this problem, but it wouldn't catch "MyPermision" outside the initial position.
You can fix this by using Contains(...):
// Construct a MyService.UserPermissions object for searching
var targetPermission = new MyService.UserPermissions("MyPermission");
var users = UserRepository
    .GetUser()
    .Where(u => u.Permissions.Contains(targetPermission))
    .ToList();

Edit: Since it turns out that you are looking for a permission with a specific PermissionName, you cannot use Contains. You need to use Any instead:
var users = UserRepository
    .GetUser()
    .Where(u => u.Permissions.Any(p => p.PermissionName == "MyPermission"))
    .ToList();


A: Make use of null propagation to skip the users without a permission object and check the array length to avoid out of bounds exceptions:
List<User> users = UserRepository.GetUser().Where(u => u.Permissions?.Length > 0 && u.Permission[0] == "MyPermission").ToList();

