Best way to remove items from a collection

前端 未结 15 2056
天命终不由人
天命终不由人 2020-12-08 05:47

What is the best way to approach removing items from a collection in C#, once the item is known, but not it\'s index. This is one way to do it, but it seems inelegant at be

15条回答
  •  忘掉有多难
    2020-12-08 06:52

    A lot of good responses here; I especially like the lambda expressions...very clean. I was remiss, however, in not specifying the type of Collection. This is a SPRoleAssignmentCollection (from MOSS) that only has Remove(int) and Remove(SPPrincipal), not the handy RemoveAll(). So, I have settled on this, unless there is a better suggestion.

    foreach (SPRoleAssignment spAssignment in workspace.RoleAssignments)
    {
        if (spAssignment.Member.Name != shortName) continue;
        workspace.RoleAssignments.Remove((SPPrincipal)spAssignment.Member);
        break;
    }
    

提交回复
热议问题