Reputation: 51927
I have the following code:
if (ListOfMyModel.Any(a => a.SomeID != 0 &&
IsAuthorizedOnID(TheUserID, a.SomeID) == false) == false)
{
return true;
}
Basically, I have a list of objects and I replaced a foreach
loop with .Any()
to which I'm passing in a lambda expression.
This code should only return true only if a) all the objects that have SomeID not equal to 0 are authorized in the second function but don't worry about the authorization for the objects that have SomeID equal to 0.
Is my expression correct or are there cases where this may fail?
Thanks.
Upvotes: 2
Views: 223
Reputation: 28528
try:
if (!ListOfMyModel.Any(a => a.SomeID != 0 && !IsAuthorizedOnID(TheUserID, a.SomeID))
or
return ListOfMyModel.All(a => a.SomeId == 0 || IsAuthorizedOnID(TheUserID, a.SomeID));
Upvotes: 0
Reputation: 887225
Instead of adding all of that negation, you should write in code exactly what you wrote in your description:
If every user has ID zero or is authorized.
return list.All(a => a.SomeId == 0 || IsAuthorizedOnID(TheUserID, a.SomeID));
Upvotes: 4