views:

275

answers:

1

I am trying to perform a query using linq to entities to that an entity/table doesn't contain the same values before I update it.

The structure of the database is as follows:

Users             User_IPAddresses          IPAddresses
-----             ----------------          -----------
UserID   >------  UserID           ------<  IPAddressID
User              IPAddressID               Address

So, the structure of the entity object is as follows

UserSet          IPAddressSet
-------  >-----< ------------
User             IPAddress

All the ID fields are Primary Keys, so the link table (User_IPAddresses) must contain unique rows.

The problem I am having is that I can't get my head around how to check the entities so that I don't violate the unique row constraint on the User_IPAddresses table before I update it.

Any EF gurus out there that can help me?

+1  A: 
//returns true if pair exists
public bool CheckIfUserIPPairExists(int ipID, int userID)
{

   bool exists 
      = db.UserSet.Any(user=>user.UserID==userID 
                             && user.IPAddress.Any(ip=>ip.IPAddressID == ipID));
   return exists;
}
Tion
Tion you are a genius, thank you
Calanus