3

I have problem with Dictionary(buzzCompaignsPerUserIntersets). I have dictionary (key = stringand value = ICollection), I want to remove from value of each key, campaign which verify condition here is the code that I used:

buzzCompaignsPerUserIntersets = Dictionary<string, ICollection<Buzzcompaign> ;

foreach(var dic_compaign in buzzCompaignsPerUserIntersets)
{
    
     var listCompaign = buzzCompaignsPerUserIntersets[dic_compaign.Key];
     for (int i = 0; i < listCompaign.Count(); i++)
     {
         if (listCompaign.ElementAt(i).MayaMembership.MayaProfile.MayaProfileId == profile_id)
                         buzzCompaignsPerUserIntersets[dic_compaign.Key].Remove(listCompaign.ElementAt(i));         
      }                
}

with this code I have a strange result because I iterate over a dictionary from which I remove elements.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ucef
  • 557
  • 3
  • 10
  • 27

2 Answers2

2

The use of ElementAt(i) is not the ideal way to get a particular item and will perform poorly. Its usage suggests that you want a collection with an indexer, such as IList<T>.

Using your current setup, you could use this approach:

foreach(var key in buzzCompaignsPerUserIntersets.Keys)
{
     var list = buzzCompaignsPerUserIntersets[key];
     var query = list.Where(o => o.MayaMembership
                                  .MayaProfile.MayaProfileId == profile_id)
                     .ToArray();
     foreach (var item in query)
     {
         list.Remove(item);
     }
}

Alternately, if you can change the ICollection<T> to an IList<T> you could use the indexer and the RemoveAt method. That would look like this:

foreach(var key in buzzCompaignsPerUserIntersets.Keys)
{
     var list = buzzCompaignsPerUserIntersets[key];
     for (int i = list.Count - 1; i >= 0; i--)
     {
         if (list[i].MayaMembership.MayaProfile.MayaProfileId == profile_id)
         {
             list.RemoveAt(i);
         }
     }
}

A List<T> would let you use the RemoveAll method. If you're interested in how that works take a look at my answer to another question.

Community
  • 1
  • 1
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
0

try something like this

foreach(var dic_compaign in buzzCompaignsPerUserIntersets) 
{ 
   buzzCompaignsPerUserIntersets[dic_compaign.Key].RemoveAll(
     dic_campaign.Value.FindAll(
       delegate(ListCampaignType item) 
       { return item.MayaMembership.MayaProfile.MayaProfileId == profile_id; })
   );
} 

ListCampaignType being the type of value in your dictionary.

Basically you can't change a collection you are iterating through so a long hand way of doing the above would be.

foreach(var dic_compaign in buzzCompaignsPerUserIntersets) 
{ 
   List<ListCampaignType> itemstoremove = new List<ListCampaignType>();
   foreach(var item in buzzCompaignsPerUserIntersets[dic_compaign.Key])
   {
      if (item.MayaMembership.MayaProfile.MayaProfileId == profile_id)
      {
         itemstoremove.Add(item);
      }
   }
   buzzCompaignsPerUserIntersets[dic_compaign.Key].RemoveAll(itemstoremove);
}
Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • ListCompaign is a list of object, not object, i try the second solution but RemoveAll is not accepted by buzzCompaignsPerUserIntersets[dic_compaign.Key] – ucef Jul 06 '12 at 17:08
  • Well crap. Just noticed you've defined it as an ICollection. Is there some specific reason for that? – Tony Hopkinson Jul 06 '12 at 19:00