Move item from one collection to another

Move item from one collection to another

Old forum URL: forums.lhotka.net/forums/t/722.aspx


Shazam posted on Tuesday, July 25, 2006

I have a use case where I need to move a child object from one BusinessListBase collection to another.  I'm currently doing this using this code:
 
collectionA.Remove(item);
collectionB.Add(item);
 
The problem with this is that this results in a DataPortal.Delete() and then a DataPortal.Add() operation, when in reality I need to do a DataPortal.Update()
 
The thing is, I'm struggling with how I can make this work.  Does anybody have any suggestions?

skagen00 replied on Tuesday, July 25, 2006

I was kind of running into something similar (swapping items within a list) and was educated on perhaps trying to solve my problem another way.

What I had done is removed the two items from the deletedlist manually after swapping their positions, with SwapPositions being a method of the collection to handle this behavior for me.

There are manual ways to get around this issue - you could remove the item in collection A from the deleted list of collection A and do something similar to the item as you add it to collection B to prevent an add (MarkDirty?). Though it doesn't seem faithful to the framework.

What is your problem that has you moving items from one collection to the other?

 

Shazam replied on Tuesday, July 25, 2006

Basically I have a tree structure that I let users edit.
 
What I set up was a collection of objects, where each object could then also have a child collection that also contained the same type of objects.  I'd let users move an object from one parent to another.
 
I solved the issue by totally sidestepping the problem.  I was using just one collection before, which each object just containing its parent's ID.  That made it really easy to move an object from one parent to another (just change the ID).  The problem was when I'd add a new object and then assigned an existing object to that new object.  I had to make sure that the new object was inserted first before the existing object was updated.  I had a naive Find() method that simply looped through the collection so I could find the parent object.  As you'd think, this meant for not small (~300 items) collections it took a very long time to find relevant object.
 
To solve that issue, I used a hashtable to facilitate the Find() method.
 
For my main issue, I went back to using a single collection.  Within the object's Update() and Insert() methods, I checked to see if the parent object was dirty.  If so, I inserted/updated it first.

Copyright (c) Marimer LLC