Entitycollection merge

Posts   
 
    
wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 23-Jun-2005 14:46:06   

Is there a way to merge two entitycollections? I'd like to do something like this:


EntityCollection collection1 = new EntityCollection(...)
EntityCollection collection2 = new EntityCollection(...)

collection1.Merge(collection2);

Of course, the method name may be different (e.g. AddRange to keep it in line with many other types). Is there a way to do this by adding or modifying a template?

Posts: 112
Joined: 09-Aug-2004
# Posted on: 23-Jun-2005 15:22:07   

When you fetch your collection you can pass the same entity collection to both adapter fetch calls. It will fetch into the collection giving you basically a merged collection.

This may work for your scenerio.

You could also write a little method,

foreach (IEntity2 e in entityCollection1) entityCollection1.Add(e);

wvnoort
User
Posts: 96
Joined: 06-Jan-2005
# Posted on: 23-Jun-2005 15:56:34   

lethologica wrote:

When you fetch your collection you can pass the same entity collection to both adapter fetch calls. It will fetch into the collection giving you basically a merged collection.

This may work for your scenerio.

My scenario is a little more complicated: 1. Fetch collection 1 and do some processing on collection 1 2. Fetch collection 2 and do some processing on collection 2 3. Merge the collections, do some final processing and send the merged collection to the reporting system.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 23-Jun-2005 16:07:38   

You can first fetch both collections, then set entitycollection1.DoNotPerformAddIfPresent to true and add all elements of entitycollection2 to entitycollection1 in a tight loop using entitycollection1.Add(entitycollection1[index])

If the collection VERY large, i.e thousands of entries, this might be slow (i.e. noticable), due to the fact that for every entity to add, the complete list of entities is searched if it's already there.

These checks are done on PK value. So new entities will always be added to entitycollection1 if they're present in entitycollection2.

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 23-Jun-2005 17:08:26   

pmfji here, but I need to do something similar but different. Instead of merging to collections I need to find the difference between two collections. So for example I have a "all available books" collection and "selected books" collection. I need to find all available books that have not been selected and build a collection. Is there a recommended way to do this via llblgen?

Thanks, Eric

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Jun-2005 12:42:55   

Implement your own hashtable based lookup routine (pretty simple: per hashcode returned by an entity, store an arraylist with the actual instances of the entities, then store all selected entities in that hashtable structure. Then loop through the total booklist and grab per entity the hashcode, check if that hashcode is present as key, if not -> not selected, if so, check if the instance is in the arraylist (indexof will do simple_smile ). If so, -> selected, if not -> not selected.

Frans Bouma | Lead developer LLBLGen Pro
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 21-Dec-2008 08:39:04   

winforms,dotnet2,vb,llblgen 2.6,adapter

Not sure if my scenario is the same.

I want to merge 2 collections(A and B).

Want to merge B into A collection.

  1. If Item in B not in A then Add it. (easy using the code below)
  2. If item in B exists in A then compare DateModified and replace DateModified is different.

Step 1. Code oIssues.DoNotPerformAddIfPresent = True For Each oIssue As VwIssuesEntity In oFetchIssues oIssues.Add(oIssue) Next

Step 2....how do i achieve this?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Dec-2008 20:10:38   

I think you should do it manually:

oIssues.DoNotPerformAddIfPresent = True
For Each oIssue As VwIssuesEntity In oFetchIssues
    
     ' search for the current issue
     Dim filter As New IPredicate(IssueFields.Id == oIssue.Id)
     Dim indexes As ArrayList  = oIssues.FindMatches(filter);

     If indexes.Count > 0 Then  
          If oIssues[0].DateModified <> oIssue.DateModified
     Else
          oIssues.Add(oIssue)
     End If
Next

P.S. For future posts, please don't reopen old threads wink

David Elizondo | LLBLGen Support Team
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 21-Dec-2008 23:56:40   

This code doesn't work..should it be ....

                oIssues.DoNotPerformAddIfPresent = True
                For Each oIssue As VwIssuesEntity In oFetchIssues
                    ' search for the current issue
                    Dim filter As IPredicate = (VwIssuesFields.IssueId = oIssue.IssueId)
                    Dim indexes As List(Of Integer) = oIssues.FindMatches(filter)

                    If indexes.Count > 0 Then
                        If oIssues(indexes(0)).DateModified <> oIssue.DateModified Then
                            oIssues(indexes(0)) = oIssue **
                        End If
                    Else
                        oIssues.Add(oIssue)
                    End If
                Next

** does not update the row in the grid?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Dec-2008 07:04:14   

Yep. You're right, I wrote an un-tested approximate code snippet just to get the idea that the process should be done manually simple_smile

David Elizondo | LLBLGen Support Team