Get entity instance from collection

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 17-Jan-2006 19:59:28   

I have a entity collection and would like to get an instance of an entity that is related to the entities in the collection.

Say I have the entity ProductVersion which has the related entity Product. I then get a collection of ProductVersion entities with a prefetch for Product entities. I then want to get an instance of Product from the collection of ProductVersion. The one catch I have to this is that I'm using reflection to return different types so the type ProductVersion could be something different. The only thing about this that is known for sure is that the relation ship between the entity type in the collection is always the same. I thought I might be able to use something like EntityBase2.GetRelatedEntity as opposed to EntityBase2.SetRelatedEntity, but there is no GetRelatedEntity method.

Now the reason for all this is so that I can create a UnitOfWork instance to use for deleting the entities I'm trying to get. Below is a sample of code that I have so far.

                int productId = (int)selectedRow.DataKeyValue;
                ProductEntity product = new ProductEntity(productId);
                product.IsNew = false;

                EntityCollection parts = ServiceFactory.GetProductManager().GetPartDetails(productType, productId);
                //EntityBase2 entity = (EntityBase2)parts[0];

                UnitOfWork2 workUnit = new UnitOfWork2();
                workUnit.AddCollectionForDelete(parts);
//              workUnit.AddCollectionForDelete(productVersions);
                workUnit.AddForDelete(product);
                ServiceFactory.GetPersistanceManager().SaveUnitOfWork(workUnit);
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jan-2006 07:00:33   

As a general rule: to get an entity instance out of a collection of entities having the same type:

MyEntity myentity = (MyEntity)MyEntityCollection.Items[i];