Comparing New Entities

Posts   
 
    
pcoulter
User
Posts: 14
Joined: 11-Nov-2004
# Posted on: 14-Apr-2006 00:39:59   

I have a situation where I have a new entity. This entity is added to two new collections, most likely at different indexes. If I need to remove the entity from the first collection, then I need to look through the second collection and remove it from there as well. I've seen references to using Entity.Equals(Entity), but it compares primary keys, which aren't set on these entities, because they are new, and the pk is an identity field. Is there a way to do what I'm trying here? (I'm using self-servicing.)

Thanks, Pete

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 14-Apr-2006 02:41:59   

Are the indexes identity columns? If not then you would be able to set the primary key before inserting them in the collection. This is what is done with Guids as PKs. If not then you can loop through the collection and use any information that would identify the New entity that you are looking for.

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 14-Apr-2006 03:47:44   

In VB.NET you can use the 'Is' keyword to compare instances. In C# I believe it is '=='. So,


VB.NET
If entity1 Is collection(index) Then
    collection.Remove(entity1)
End If

C#
If (entity1 == collection[index])
{
    collection.Remove(entity1);
}

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Apr-2006 09:15:33   

I think the Is keyword checks if an object is of a given type, at least that how it is used in C#.

If the same entity instance exists in both collection. Then to find it in one collection of them just use the following:

int nIndex = Collection.IndexOf(entityToRemove)

This will return the index of the entity you want to remove, if it's found there.

And for boolean checking:

bool bFound = Collection.Contains(entityToRemove)
mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 14-Apr-2006 22:18:51   

Walaa wrote:

I think the Is keyword checks if an object is of a given type, at least that how it is used in C#.

If the same entity instance exists in both collection. Then to find it in one collection of them just use the following:

int nIndex = Collection.IndexOf(entityToRemove)

This will return the index of the entity you want to remove, if it's found there.

And for boolean checking:

bool bFound = Collection.Contains(entityToRemove)

'Is' is used to compare memory instances in VB.NET. Like I said, I think it is '==' in C#. I could be wrong, but I think IndexOf and Contains use the .Equals of the entity, which will not work in his case because his primary keys are not set in the entities he is looking for. When PKs are not set in the entities, Equals uses the field values...this is not accurate for finding an instance of an entity.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 15-Apr-2006 11:56:48   

mikeg22 wrote:

Walaa wrote:

I think the Is keyword checks if an object is of a given type, at least that how it is used in C#.

If the same entity instance exists in both collection. Then to find it in one collection of them just use the following:

int nIndex = Collection.IndexOf(entityToRemove)

This will return the index of the entity you want to remove, if it's found there.

And for boolean checking:

bool bFound = Collection.Contains(entityToRemove)

'Is' is used to compare memory instances in VB.NET. Like I said, I think it is '==' in C#.

Correct.

I could be wrong, but I think IndexOf and Contains use the .Equals of the entity, which will not work in his case because his primary keys are not set in the entities he is looking for. When PKs are not set in the entities, Equals uses the field values...this is not accurate for finding an instance of an entity.

No that's not correct. When one of the two entities compared is new, the Equals implementation does an instance compare. If both are not new, it does a PK field values compare. It falls back to instance compare for new entities because the PK values aren't 'finalized' and can be identity values or set later on.

Frans Bouma | Lead developer LLBLGen Pro
pcoulter
User
Posts: 14
Joined: 11-Nov-2004
# Posted on: 18-Apr-2006 02:29:00   

Walaa wrote:

If the same entity instance exists in both collection. Then to find it in one collection of them just use the following:

int nIndex = Collection.IndexOf(entityToRemove)

This will return the index of the entity you want to remove, if it's found there.

The reason this doesn't work is because the entity instance is not necessarily added to each collection at the same index.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Apr-2006 08:36:55   

The reason this doesn't work is because the entity instance is not necessarily added to each collection at the same index.

I don't get it, you have an entity instance from Collection1, and you want to find it in Collection2, then

int nIndex2 = Collection2.IndexOf(entityFromCollection1)
mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 18-Apr-2006 16:57:48   

Walaa wrote:

The reason this doesn't work is because the entity instance is not necessarily added to each collection at the same index.

I don't get it, you have an entity instance from Collection1, and you want to find it in Collection2, then

int nIndex2 = Collection2.IndexOf(entityFromCollection1)

Yeah, I guess this would work because of what Frans said...Equals defaults to an instance check for IsNew entities. simple_smile