Best way to remove items from a collection

前端 未结 15 2028
天命终不由人
天命终不由人 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:51

    There is another approach you can take depending on how you're using your collection. If you're downloading the assignments one time (e.g., when the app runs), you could translate the collection on the fly into a hashtable where:

    shortname => SPRoleAssignment

    If you do this, then when you want to remove an item by short name, all you need to do is remove the item from the hashtable by key.

    Unfortunately, if you're loading these SPRoleAssignments a lot, that obviously isn't going to be any more cost efficient in terms of time. The suggestions other people made about using Linq would be good if you're using a new version of the .NET Framework, but otherwise, you'll have to stick to the method you're using.

提交回复
热议问题