4

I'm trying to modify each value in a Dictionary. The answers about how to do that generally look like this (taken from here):

foreach (var key in myDictionary.Keys.ToList())
{
    myDictionary[key] = <some kind of new value>
}

Seemingly straightforward, but it doesn't work for me; Visual Studio underlines ToList() and says

'System.Web.UI.WebControls.QueryableDataSourceHelper.ToList(System.Linq.IQueryable, System.Type)' is inaccessible due to its protection level.

Am I doing something wrong? (Obviously yes, but what?)

Community
  • 1
  • 1
Beska
  • 12,445
  • 14
  • 77
  • 112
  • regenerate your designer.cs file for the aspx – qamar May 29 '14 at 15:24
  • 2
    Do you have `using System.Linq;`? – Jon May 29 '14 at 15:24
  • 1
    @Vlad He would be modifying a collection while enumerating it. – Servy May 29 '14 at 15:26
  • And what is the compile-time type of `myDictionary`? – Jon Skeet May 29 '14 at 15:26
  • @Jon Add that as an answer, and I'll give you the win. I added it and all is immediately fine. Didn't immediately realize it was a Linq thing (since it seemed to grok what I was attempting without it, rather than just complaining that I was missing a library). – Beska May 29 '14 at 15:41
  • @Beska: I don't mind. But if I could do anything, I would prefer that this question be removed entirely because there's virtually no chance that it will help someone in the future -- even if someone has the exact same problem, they won't be able to find it. – Jon May 29 '14 at 22:58
  • @jon Unfortunately, the site complains when I try to remove it, because it has been answered. It threatens me with potentially blocking me from asking more questions because I'm trying to remove a question with an answer. – Beska Jun 21 '14 at 08:02
  • @Beska: Then don't. Or perhaps flag it for moderator attention and briefly explain that this particular question is not useful to the site's audience as a whole. Either way don't lose any sleep over it. – Jon Jun 21 '14 at 09:21
  • 1
    Actually, it can be helpful. Google brought me here from a "KeyCollection ToList" search. Old code being updated has these annoying missing things. – Edyn Jul 23 '14 at 21:41

1 Answers1

12

This is fixed by adding a

using System.Linq;

to the top of the .cs file.

(Jon, since you led me to the right path, if you want to post this as an answer, do so, and I'll erase this answer and give you credit.)

Beska
  • 12,445
  • 14
  • 77
  • 112