Copying a collection

Posts   
 
    
SQLScott
User
Posts: 2
Joined: 18-Mar-2005
# Posted on: 18-Mar-2005 21:31:52   

Please forgive the potentially easy question, as I am new to LLBLGen. I am trying to clone/copy a collection.

Looking in the reference manual I see that my best option is the CopyTo method. However, it does seem to be working as I thought it might. The error I receive is the following:

Value of Type Csi.CsiBusinessBase.CollectionClasses.MenuRolesCollection cannot be converted to a 1-dimensional array of SD.LLBLGen.Pro.ORMSuportClasses.IEntity.

My code basically looks like the following:

Private _MenuRoles As CollectionClasses.MenuRolesCollection = New CollectionClasses.MenuRolesCollection

Private _NewMenuRoles As CollectionClasses.MenuRolesCollection = New CollectionClasses.MenuRolesCollection

Dim filter As IPredicateExpression = New PredicateExpression

filter.Add(PredicateFactory.CompareValue(MenuRolesFieldIndex.RoleId, ComparisonOperator.Equal, RoleID))

_MenuRoles.GetMulti(filter)

_MenuRoles.CopyTo(_NewMenuRoles, 0) <-- Error is here..

Any thoughts and feedback is greatly appreciated.

Scott

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 19-Mar-2005 11:19:56   

If you want to clone everything, you should use the BinaryFormatter, serialize the collection to a MemoryStream, then do memoryStream.Seek(0, SeekPosition.Begin) (please look up the exact syntaxis) and deserialize the memorystream into a new collection using the binaryformatter class. Then you'll have a complete clone including cloned entity objects.

Frans Bouma | Lead developer LLBLGen Pro
IowaDave
User
Posts: 83
Joined: 02-Jun-2004
# Posted on: 25-Mar-2005 15:58:16   

To show the syntax, here's an example - it works great - but slooow - flushed (mine was about 7MB of data)

    MemoryStream m_stream = new MemoryStream();
    // Construct a BinaryFormatter and use it to serialize the data to the stream.
    BinaryFormatter m_formatter = new BinaryFormatter();

    EntityCollection  companyCollection = 
             new  EntityCollection(new CompanyEntityFactory());
    DataAccessAdapter adapter = new DataAccessAdapter();            
    adapter.FetchEntityCollection(companyCollection, null);         
     try 
     {
         m_formatter.Serialize(m_stream, companyCollection);
     }
     catch (Exception e) 
     {
          Console.WriteLine("Failed to serialize. Reason: " + e.Message);
           throw;
     }
     finally 
     {
           m_stream.Seek(0,SeekOrigin.Begin);
     }      
     // create a new collection and fill it from memorystream 
     EntityCollection copyOfCompanyCollection = 
            new  EntityCollection(new  CompanyEntityFactory());
     copyOfCompanyCollection  = (EntityCollection) m_formatter.Deserialize(m_stream);
     // Print the names just to see if it worked. 
     foreach(CompanyEntity companyEntity in copyOfCompanyCollection)
     { 

            Debug.WriteLine(companyEntity.CompanyName);
     }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 25-Mar-2005 16:47:58   

Be aware that you clone the complete graph in memory, every object referenced by an entity is cloned. If you've loaded a lot of data into memory, this indeed can be slow. In that case, simple refetch the entities you want to clone into new objects.

Frans Bouma | Lead developer LLBLGen Pro