4

Trying to do a foreach:

foreach(User in userList)
{

}

Where userList is a IEnumerable<User> userList

I tried doing: userList.ToList() but I get the same message.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
codecompleting
  • 9,251
  • 13
  • 61
  • 102

4 Answers4

9

In your foreach statement, you haven't specified an identifier for the current instance of User. Try adding an identifier (e.g., currUser or just user) after the type User, like this:

foreach(User user in userList)
{

}
Donut
  • 110,061
  • 20
  • 134
  • 146
5

the other answers showed you your syntax problem, but some tool (I think resharper) will warn you that you might enumerate the sequence more than once (maybe even VS - don't know because I use them together). If that is the problem write something like

var userArray = userList.ToArray();

and use userArray instead of userList

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • 1
    see R# documentation: [link](http://confluence.jetbrains.net/display/ReSharper/Possible+multiple+enumeration+of+IEnumerable) – springy76 Sep 12 '11 at 16:03
4

It should be:

foreach(User user in userList)
{

}
zapico
  • 2,396
  • 1
  • 21
  • 45
3

A foreach loop requires you to define a variable to place each iteration in. You have only defined the class and are missing the variable.

foreach(User _user in userList)
{

}
John Hartsock
  • 85,422
  • 23
  • 131
  • 146