Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to deep-copy the ListCollectionView?

How to deep-copy the ListCollectionView?

Scheduled Pinned Locked Moved C#
questioncsharptutorial
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Sync
    wrote on last edited by
    #1

    I'd like to deep-copy the ListCollectionView. How can I do that? I have tried several techniques but the most of techniques that I tried are not working. Any idea would be appreciated.

    Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

    L A 2 Replies Last reply
    0
    • M Michael Sync

      I'd like to deep-copy the ListCollectionView. How can I do that? I have tried several techniques but the most of techniques that I tried are not working. Any idea would be appreciated.

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Deep copy, as in clone the objects too?

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      M 1 Reply Last reply
      0
      • L leppie

        Deep copy, as in clone the objects too?

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        M Offline
        M Offline
        Michael Sync
        wrote on last edited by
        #3

        yes but i dont want to have an reference of the original object. For example ~ List persons = new List; public void populateDataAndBindItToTreeView(){ ///......... } public void UpdateData(){ List newPersons = new List; /// Do something here. DeepCopy(persons, newPersons); // will update the persons and treeview. } Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

        L N 2 Replies Last reply
        0
        • M Michael Sync

          yes but i dont want to have an reference of the original object. For example ~ List persons = new List; public void populateDataAndBindItToTreeView(){ ///......... } public void UpdateData(){ List newPersons = new List; /// Do something here. DeepCopy(persons, newPersons); // will update the persons and treeview. } Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Michael Sync wrote:

          yes but i dont want to have an reference of the original object.

          Which object, the list or the contents? Is Person clonable?

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          M 1 Reply Last reply
          0
          • L leppie

            Michael Sync wrote:

            yes but i dont want to have an reference of the original object.

            Which object, the list or the contents? Is Person clonable?

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            M Offline
            M Offline
            Michael Sync
            wrote on last edited by
            #5

            leppie wrote:

            Which object, the list or the contents?

            Yes. the list of person.

            leppie wrote:

            Is Person clonable?

            No. (Actually, I'm using ListCollectionView instead of List in my project. AFAIK, ListCollectionView is not a clonable ) Thanks.

            Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

            L 1 Reply Last reply
            0
            • M Michael Sync

              leppie wrote:

              Which object, the list or the contents?

              Yes. the list of person.

              leppie wrote:

              Is Person clonable?

              No. (Actually, I'm using ListCollectionView instead of List in my project. AFAIK, ListCollectionView is not a clonable ) Thanks.

              Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Michael Sync wrote:

              leppie wrote: Is Person clonable? No.

              Then it will be a shallow clone, with new List<Person>(yourlistcollectionview), you maybe have to cast/convert the ListCollectionView' IEnumerable to a IEnumerable<Person> first.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              M 1 Reply Last reply
              0
              • M Michael Sync

                I'd like to deep-copy the ListCollectionView. How can I do that? I have tried several techniques but the most of techniques that I tried are not working. Any idea would be appreciated.

                Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                A Offline
                A Offline
                Alaric_
                wrote on last edited by
                #7

                (I'm not sure what a "ListCollectionView" is, but I can give some general information on creating a deep-copy of an object graph) well...there are several approaches you could take. All have their pros and cons...here's one technique. Mark all of your objects in your object graph with the Serializeable attribute. There are 2 noticeable cons to this technique: One- you have to mark each class individually, so there is a chance that one could be accidentally omitted. Two- You don't want to serialize objects that contain references to external resources (databaseConnections being a prime example)

                Your procedure (in pseudo-code)

                Create a BinaryFormatter
                Create a MemoryStream to store the serialized buffer of your object graph
                formatter.Serialize(buffer, source) //the only line of code I'll give
                return the clone by Deserializing the buffer and applying a C-cast to the stream.
                The Serializeable attribute will take care of the rest

                The objects in the graph DO NOT have to be ICloneable. The Serializeable attribute is all that is needed. You do, however need to mark anything that you do not want to be contained in the clone as Transient

                "I need build Skynet. Plz send code"

                M 1 Reply Last reply
                0
                • M Michael Sync

                  yes but i dont want to have an reference of the original object. For example ~ List persons = new List; public void populateDataAndBindItToTreeView(){ ///......... } public void UpdateData(){ List newPersons = new List; /// Do something here. DeepCopy(persons, newPersons); // will update the persons and treeview. } Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  Iterate through each items in the persons collection, create new items with the same value and add to newPersons. This will be the most easiest way. Something like

                  foreach(Person person in persons)
                  {
                  newPersons.Add(new Person(person.Name,person.Email,...));
                  }

                  You will have new instances with same value in the newPersons collection. Is this what you are looking for or I got you wrong ?

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

                  1 Reply Last reply
                  0
                  • A Alaric_

                    (I'm not sure what a "ListCollectionView" is, but I can give some general information on creating a deep-copy of an object graph) well...there are several approaches you could take. All have their pros and cons...here's one technique. Mark all of your objects in your object graph with the Serializeable attribute. There are 2 noticeable cons to this technique: One- you have to mark each class individually, so there is a chance that one could be accidentally omitted. Two- You don't want to serialize objects that contain references to external resources (databaseConnections being a prime example)

                    Your procedure (in pseudo-code)

                    Create a BinaryFormatter
                    Create a MemoryStream to store the serialized buffer of your object graph
                    formatter.Serialize(buffer, source) //the only line of code I'll give
                    return the clone by Deserializing the buffer and applying a C-cast to the stream.
                    The Serializeable attribute will take care of the rest

                    The objects in the graph DO NOT have to be ICloneable. The Serializeable attribute is all that is needed. You do, however need to mark anything that you do not want to be contained in the clone as Transient

                    "I need build Skynet. Plz send code"

                    M Offline
                    M Offline
                    Michael Sync
                    wrote on last edited by
                    #9

                    Alaric_ wrote:

                    (I'm not sure what a "ListCollectionView" is

                    ListCollectionView[^] Yes. Binary Serialization would be great.. I found an example that shows how to deep-copy the object using XmlFormater but that guy said that we should consider using Binary Serialization... But the problem is that there is no [Serializeable] attribute in ListCollectionView. and I can't modify the code since ListCollectionView is a part of .NET framework..

                    Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                    1 Reply Last reply
                    0
                    • L leppie

                      Michael Sync wrote:

                      leppie wrote: Is Person clonable? No.

                      Then it will be a shallow clone, with new List<Person>(yourlistcollectionview), you maybe have to cast/convert the ListCollectionView' IEnumerable to a IEnumerable<Person> first.

                      xacc.ide - now with TabsToSpaces support
                      IronScheme - 1.0 alpha 4a out now (29 May 2008)

                      M Offline
                      M Offline
                      Michael Sync
                      wrote on last edited by
                      #10

                      Sorry for making you confused. Let me elaborate it again.. This is our real scenario... We are using WPF and Data-binding. We are using ListCollectionView to bind the control (e.g. Datagrid, TreeView or etc) ... The reason why we are using ListCollectionView is that it supports sorting, filtering and etc... So, our code will be like that. Note: We are using MVVM pattern in our project so we dont want to write the code in code-behind. We wrote the most of code in ViewModel and bind that ViewModel with View.. ===== Class ===== class Person() : INotification { private int _id; private string _Name; private List myPets = new List; public int ID{ /// } public string Name{ /// } public List MyPets{ // } //impelementation for INotification. } then.. I bind it with TreeView. =========== View - XAML =========== Souce="Pet" // child class ... /> .... Pet.Name /> ItemSouce={... personListViewCollection } //personListViewCollection is the object from code below ... /> =========== ViewModel =========== ListViewCollection personListViewCollection ; void Constructor(){ populateData(); } void populateData(){ List persons = new List(); Person p1 = new Person(); p1.ID =1; .. List pets = new List(); pets.Add(new Pet(){ ...... } ); pets.Add(new Pet(){ ...... } ); p1.MyPets.AddRange(pets); person.Add(p1); /// add p2 p3 p4 ///note: // Let's say personListViewCollection.GetHashCode() == 1000100 personListViewCollection = new ListViewCollection(persons); } So, those data will be displayed on TreeView.. then, I wanna re-populatethe data. void reload(){ List newPersons = new List(); Person newP1 = new Person(); newP1.ID =1; .. List newPets = new List(); newPets.Add(new Pet(){ ...... } ); newPets.Add(new Pet(){ ...... } ); newP1.MyPets.AddRange(newPets); newPersons.Add(newP1); ///HERE is the problem. I dont want the hashcode of personListViewCollection to change. personListViewCollection = new ListViewCollection(newPersons); //Problem.. } then, I want to refresh the treeview and display the new data... Any Idea?

                      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups