I want to check if any elements in 2 lists are the same based on a specific property, then just return true, false.
Right now I have:
public bool CanEdit(List<RoleType> roles)
{
var rolesThatCanEdit = new List<RoleType>{
RoleType.Administrator,
RoleType.Editor
};
//check if the roles can edit.
return rolesThatCanEdit.Intersect(roles).Any();
}
But my guess how this works is that it will make a new list then just check if anything is in the list. Is there a way I can just return true on the first matched element? worst case is there is no matching elements and it will loop through the entire list internally.
Not only will it not compute the complete intersection, it will turn the second input list (the one that’s not a
thisparameter on the extension method) into a hashtable to enable very fast repeated lookups. (There may be a performance difference betweenrolesThanCanEdit.Intersect(roles)vsroles.Intersect(rolesThatCanEdit))