2

I have a entity class that it name is Person. The Person entity has a list of Asset entity.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public IList<Asset> Assets { get; set;}
}

For example I have two Person (person1 & person2). I need to copy list of assets from person1 to person2. I do it like following code :

Person person1 = LoadById(1);
Person person2 = LoadById(2);

// person2.Assets = person1.Assets;   // Is it correct?

foreach(Asset item in person1.Assets)
{
    //person2.Assets.Add(item);
    Asset asset = new Asset();
    asset.Title = item.Title;
    asset.Description = item.Description;

    asset.Person = person2;        

    person2.Add(asset);
}    

person2.Update();

There is a better way to do this?

Ehsan
  • 3,431
  • 8
  • 50
  • 70
  • 1
    This will add the same item reference to the second list. If this is not the desired behaviour you have to clone/deep clone the item before adding it to the assets list. – Dennis Dec 21 '11 at 11:24
  • This one is great: http://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp – Dennis Dec 21 '11 at 14:09

2 Answers2

3

The Assets property is a reference to a list. person2.Assets = person1.Assets; would mean they both share and point to the same list, so would not be separate.

You want to do :

person2.Assets = new List<Asset>(person1.Assets);

(Just going to check that overload, but that should copy all the contents)... Yes, that should do it.

If you wanted to add them rather than replace, use this:

person2.Assets.AddRange(person1.Assets);

You might get duplicates, however. You could do this if you don't want that: Linq to entities : Unions + Distinct

Also, you have an additional () - your first line should just be, public class Person.

Community
  • 1
  • 1
Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
  • What is diffrence between `person2.Assets = person1.Assets` and `person2.Assets = new List(person1.Assets)`? Both have one result. Both share point to the same list. I want copy a list of Asset from person1 to Assets in person2 by new ID. Now both ID for Assets[0] in person1 and person2 is equal. – Ehsan Dec 21 '11 at 13:54
  • 1
    They both have different results. In `person2.Assets = person1.Assets;` there is ONE list of assets, and it's pointed to (shared) between person1 and person2. If you add one to person1, it will also be accessible in person2, because it's the very same list. The second line creates a duplicate copy of the list, so they can be modified independently. That is almost always what you want. – Kieren Johnstone Dec 21 '11 at 14:47
0

As a starting point I recommend this MSDN documentation of the MemberwiseClose method.

This would make your copying process a lot more readable. But note that this also just creates a shallow copy of your list.

If you really need a deep copy, the article suggests this strategies:

There are numerous ways to implement a deep copy operation if the shallow copy operation performed by the MemberwiseClone method does not meet your needs. These include the following:

  • Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.

  • Call the MemberwiseClone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. The DeepCopy method in the example illustrates this approach.

  • Serialize the object to be deep copied, and then restore the serialized data to a different object variable.

  • Use reflection with recursion to perform the deep copy operation.

Jens H
  • 4,590
  • 2
  • 25
  • 35