ChicagoKiwi wrote:
The primary keys will be different (since I'm merging two entity collections) but there will be some fields that, as defined by the business, must not be duplicated in a collection. In the example above the same contact can't be listed twice with the same role on the same project. i.e. together the projectid, contactid, and roleid must be unique. I am merging the entitycollections between projects so I know that the projectid will differ, but I want to know whether there's already an entity in the collection with the same contactid and roleid before adding an entity to the collection. I guess I could add a unique constraint on the table (which I don't currently have) and then just try adding all the entities and catching the exception, but this seems a little odd?
Still :
If the starting point is always: check if the contact already performs a given role on a given project, you could use a little trick: create a hashtable and use the contactID as the key. As value you use a 64bit number, with the roleID shifted up 32bits and or-ed with the projectid.
Then traverse the collection once and you can check easily with the hashtable if an entity is already there or not.