Here is one way
foreach (PatchFacilityManager PM in patchFacilityManager) { PM.IsSelected = facilityManagerId.Contains(PM.FacilityManagerId); }
EDIT
This solution is effective in two in three IMHO ways compared to the code given in the question.
First , it does not check the condition, and the result of the expression is immediately assigned to PM.IsSelected. According to LukeH's comment , it is not necessary to set PM.IsSelected to false, so the condition is inevitable. However, this improvement is applicable if the given should set to false. Hit>. From the answer to the question about the author, his case seems to be going right with this optimization. So there is no need for a conditional appointment.
Second , it does not iterate over the entire list, as List.Contains (int) returns true and exits the loop in the first occurrence of the int passed in the argument.
Third , when the environment provides you with List.Contains (int) functionality, then why reinvent the wheel. Thus, in terms of service, it is also more efficient.
source share