Using 'RemoveAt' - Problem

Posts   
 
    
ercwebdev
User
Posts: 9
Joined: 17-Jul-2012
# Posted on: 18-Sep-2012 17:53:10   

Hello, I'm trying to pull out data from 2 tables and compare table1 with ALL of the values in table2. When it finds a match, it adds it position to a LIST. I'm then using the list to remove the entities from the collection. Its appears to be doing something, but all of the values in the collection are then messed up. I've included a simplified comparison table below with letters of the alphabet used to map the values, so you can see the shift pattern.

No of entites in collection before: 232 No of entites after: 228

Its clearly not actually removing the values. You can see below that it has only actually deleted one - entity number 2 (value of C) The other 3 entities (6,11,17) have just been re-shuffled. Worse still, it has now actually not included some of the entites that were in the original collection, that were not flagged for removal :

Entity (before) (after) 0 A A 1 B B 2 C D 3 D E 4 E F 5 F G 6 G I 7 H J 8 I K 9 J L 10 K M 11 L O 12 M P 13 N Q 14 O R 15 P S 16 Q T 17 R V

Here is the code that I'm using to achieve this task:

void LoadData(int ID)
        {
            //Get List of Collections already added
            EntityCollection<ErcHeritageRelatedCollectionEntity> orgsadded = new EntityCollection<ErcHeritageRelatedCollectionEntity>();
            // fetch them using a DataAccessAdapter instance
            IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(ErcHeritageRelatedCollectionFields.ResourceId == ID);
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(orgsadded, filter, 0, null);
            }
            RadListBoxDestination.DataSource = orgsadded;
            RadListBoxDestination.DataTextField = "CollectionTitle";
            RadListBoxDestination.DataValueField = "UID";
            RadListBoxDestination.DataBind();
        
            
            //Get List of Collections
            SortExpression sorter = new SortExpression(ErcHeritageCollectionFields.CollectionTitle | SortOperator.Ascending);
            EntityCollection<ErcHeritageCollectionEntity> orgs = new EntityCollection<ErcHeritageCollectionEntity>();
            // fetch them using a DataAccessAdapter instance
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(orgs, null, 0, sorter);
            }

               List<int> items = new List<int>();
                foreach (IEntity2  entity1 in orgsadded)
                {
                    int i = 0;
                    int j = 0;
                    foreach (IEntity2 entity2 in orgs)
                    {   
                        if (entity1.Fields[i].CurrentValue.ToString() == entity2.Fields[1].CurrentValue.ToString())
                        {
                            string _val1 = entity1.Fields[i].CurrentValue.ToString();
                            string _val2 = entity2.Fields[1].CurrentValue.ToString();
                            litFeedback.Text += "Match found " + _val1 + " with " + _val2 + "<br />" ;
                            items.Add(j);                       
                        }
                        j++;
                    }
                    i++;
                }

                foreach (int currValue in items)
                {
                   orgs.RemoveAt(currValue);
                }

            RadListBoxSource.DataSource = orgs;
            RadListBoxSource.DataTextField = "CollectionTitle";
            RadListBoxSource.DataValueField = "CollectionID";
            RadListBoxSource.DataBind();    
        }

Any guidance on whats going wrong here would be great. I'm a relative newcomer to LLBLGen pro. Thanks, Alan

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Sep-2012 20:21:36   

foreach (int currValue in items) { orgs.RemoveAt(currValue); }

The above won't work, as when you start removing the next indexes of the collection will be different.

Example, if you remove the first item, the second one then becomes the first, and all other items are shifted one step forward, and then subsequent removes will remove wrong items.

If you want to use indexes to remove items, start y removing indexes from the end of the collection, i.e. start by the biggest index, and move descending-ly.

ercwebdev
User
Posts: 9
Joined: 17-Jul-2012
# Posted on: 19-Sep-2012 10:47:55   

That works, thanks! I knew something like this was going on, so thanks for clarifying exactly what was happening. I found this useful list sorter function that I've used:


items.Sort(new SortIntDescending()); //sort descending


private class SortIntDescending : IComparer<int>
        {
            int IComparer<int>.Compare(int a, int b) //implement Compare
            {
                if (a > b)
                    return -1; //normally greater than = 1
                if (a < b)
                    return 1; // normally smaller than = -1
                else
                    return 0; // equal
            }
        }