EntityCollection "copy" to a different entity collection

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 21-Mar-2012 11:35:15   

What is the best way to copy the data from EntityCollection<MyFirstEntity> to EntityCollection<MySecondEntity>.

The definitions of MyFirstEntity and MySecondEntity are almost identical except that MySecondEntity has some additional fields.

I'm thinking that it might be possible to do this with a projection but I can't find a direct example for this.

Right now, I'm creating an empty collection for MySecondEntity and then looping through EntityCollection<MyFirstEntity> and creating each entity for EntityCollection<MySecondEntity> in the loop. It doesn't seem like a great approach but it does work.

I'm interested in the best option for performance but also for coding simplicity.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Mar-2012 13:42:33   

You can use Linq2Objects to query the first collection, and return items of the second type.(Projection)

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 21-Mar-2012 18:29:26   

Can you give me a short code snippet? I'm not sure I understand what you are telling me.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Mar-2012 05:12:52   

jovball wrote:

Can you give me a short code snippet? I'm not sure I understand what you are telling me.

// fetch collection
var customers = new EntityCollection<CustomerEntity>();         
using (var adapter = new DataAccessAdapter())
{
    adapter.FetchEntityCollection(customers, null);
}

// copy results to another collection of a similar type
var customersExtended = new EntityCollection<CustomerExtended>();
customersExtended = new EntityCollection<CustomerExtended> 
                    (from c in customers
                        select new CustomerExtended
                        {
                            CustomerId = c.CustomerId,
                            CompanyName = c.CompanyName,
                            Country = c.Country,
                            SomeExraField1 = string.Format("Country : {0}", c.Country),
                            SomeExraField2 = string.Format("City : {0}", c.City)
                            // more fields...
                        });
David Elizondo | LLBLGen Support Team