Problem removing entity from collection

Posts   
 
    
G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 23-Aug-2007 10:35:22   

Hi,

Can anybody tell be why test = false and how I can do a compare to get it true?

Project PK: ProjectId ProjectMember PK: ProjectId, MemberId

Since I haven't saved the project yet, I have no project id ... and I can't just set the project id to 0 ... because then pme1 gets removed from the collection somehow ...

ProjectEntity pe = new ProjectEntity();

ProjectMemberEntity pme1 = new ProjectMemberEntity();
pme1.MemberId = 17;

pe.ProjectMembers.Add(pme1);

ProjectMemberEntity pme2 = new ProjectMemberEntity();
pme2.MemberId = 17;

bool test = pe.ProjectMembers[0].Equals(pme2);

And since the equals is false, I can't remove it from the collection ...

Best regards,

  • G.I.
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Aug-2007 10:44:52   

The following is copied from the Reference manual for Equals method:

If this entity or the passed in entity is new, no values are compared, but the physical objects are compared (object.Equals()), because new entities can look the same, value wise due to identity fields which are all 0, however which are physical different entities (object wise)

Since both entities are new & they are physically different, the Equals() will always return false.

You'd better use the following, if you want to compare the MemberId in both entities:

bool test = (pe.ProjectMembers[0].MemberId == pme2.MemberId);
G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 23-Aug-2007 10:55:27   

hmmm ... it's not that I want to do Equals ... it's that I want to to:

pe.ProjectMembers.Remove(pme2);

So you say I have to make a loop over the collection and check every entity?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Aug-2007 11:14:00   

In this case you should use the FindMatches() method of the entityCOllection, rather than looping through the collection

G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 23-Aug-2007 12:11:00   

yes of course, that works fine! Thank you for your help!wink