0

I have a foreach of a List and i want to Update the list or (i don´t know wich is better) create a new one with the new values. How to do this ? My code is bigger than this because i am decrypting, but if help me with this simple, will fix the other.

foreach (Envolvido envolvido in ListaDados.ItemsSource)
{
  for (int i = 0; i < ListaDados.ItemsSource.OfType<object>().Count(); i++)
  {
     var suspid = Convert.FromBase64String(envolvido.SUSPID);
     var ivnome = Convert.FromBase64String(envolvido.IVNOME);
  }
}

So, with the help of all, i got the correct answer !

 List<Envolvido> mylist = t.Result;
 for (int index = 0; index < mylist.Count(); index++)
                          {
    var items = mylist.ToList();
    Envolvido envolvido = items[index];

    envolvido.SUSPID= Convert.FromBase64String(envolvido.SUSPID);
    envolvido.IVNOME = Convert.FromBase64String(envolvido.IVNOME);
    }

THANKS!

Mcfer
  • 194
  • 1
  • 4
  • 15

1 Answers1

3

Use for for modifing lists. NOT foreach.

As it says on MSDN:

The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.

        List<Envolvido> mylist = t.Result;

         for (int index = 0; index < mylist.Count(); index++)
         {
             var items = mylist.ToList();
             Envolvido envolvido = items[index];

             envolvido.SUSPID= Convert.FromBase64String(envolvido.SUSPID);
             envolvido.IVNOME = Convert.FromBase64String(envolvido.IVNOME);
          }
Alexandr
  • 5,460
  • 4
  • 40
  • 70
  • If possible, show me adapting my code. I am newbie and like i said, i don´t know – Mcfer Aug 16 '16 at 18:46
  • Thanks!! But now i got the error "cannot apply indexing with to an expression of type ienumerable" on the line Envolvido envolvido = ListaDados.ItemsSource[index]; – Mcfer Aug 16 '16 at 19:21
  • @MarceloCFernandes Then your ListaDados.ItemsSource is not a list probably (or at least compiler doesn't know about it). Pass ListaDados as a list, not as IEnumerable. – Sergey.quixoticaxis.Ivanov Aug 16 '16 at 19:24
  • I don't know type of your ListaDados.ItemsSource but try to convert it to array or list --> ListaDados.ItemsSource.ToArray(); or ToList() before applying indexing – Alexandr Aug 16 '16 at 19:27
  • @Sergey.quixoticaxis.Ivanov all the code is based in a Ienumerable...if i change it here, i have to change a lot of code...better, try to find another way here. – Mcfer Aug 16 '16 at 19:31
  • @Alexandr could you be more especific ? i am a begginer...i tried to do, but is not working. i tried ListaDados.ItemsSource = ListaDados.ItemsSource.OfType().ToList(); and then Envolvido envolvido = ListaDados.ItemsSour‌​ce[index] but still not working – Mcfer Aug 16 '16 at 19:35
  • @Alexandr still not working... first : i changed "ListaDados.ItemsSource.Count " to "ListaDados.ItemsSource.OfType().Count()" ... and tried your code, but still with problem.. now at "var items = ListaDados.ItemsSource.ToArray();" . The ToArray() is underlined "ienumerable does not contain a definition for toarray..." – Mcfer Aug 16 '16 at 19:40
  • @MarceloCFernandes you need to find the place (outside of the snippet you provided) where you define ListaDados. Now it's defined as IEnumerable, but should be defined as concrete type List. – Sergey.quixoticaxis.Ivanov Aug 16 '16 at 20:12
  • @Sergey.quixoticaxis.Ivanov i receive the List and i do ListaDados.ItemsSource = t.Result; !! Just it... – Mcfer Aug 16 '16 at 20:20
  • Ok, got it. Then this should work (if you have List, use list, if you have ListT, use ListT): `List mylist = (List) ListaDados.ItemSource;` and then use for to iterate through `mylist`. – Sergey.quixoticaxis.Ivanov Aug 16 '16 at 20:24
  • @Sergey.quixoticaxis.Ivanov i will try in the morning on my Work and i tell you. I hope it work !! I just need this to finish my app.... – Mcfer Aug 17 '16 at 01:38
  • @Sergey.quixoticaxis.Ivanov Wich class belongs it ? because "List mylist = ...." is not possible . this is in c# ? – Mcfer Aug 17 '16 at 13:02
  • 1
    @Sergey.quixoticaxis.Ivanov thanks for the help ! I got it... i changed for List mylist = t.Result; and then for (int index = 0; index < mylist.Count(); index++) { var items = mylist.ToList(); Envolvido envolvido = items[index]; envolvido.SUSPID = Convert.FromBase64String(envolvido.SUSPID); } – Mcfer Aug 17 '16 at 13:57
  • @Alexandr thanks for the help... i edited my post with the correct answer...with your help and Sergey, i got what was wrong. – Mcfer Aug 17 '16 at 14:00
  • @MarceloCFernandes great! My bad, I meant [ArrayList](https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx) (it's an old not generic implementation of IList interface). I wanted to mention it in case you are working with some old codebase. – Sergey.quixoticaxis.Ivanov Aug 17 '16 at 18:50
  • @MarceloCFernandes also don't forget to mark Alexandr's answer =) – Sergey.quixoticaxis.Ivanov Aug 17 '16 at 18:52
  • @Sergey.quixoticaxis.Ivanov But is not the answer... he has to edit ! If someone use that answer, will not work! – Mcfer Aug 19 '16 at 14:07
  • @MarceloCFernandes no problem I have edited the answer =) – Alexandr Aug 19 '16 at 14:10