codecompleting
codecompleting

Reputation: 9611

Help with foreach, getting 'possible multiple enumeration of IEnumerable'

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.

Upvotes: 4

Views: 1021

Answers (4)

Random Dev
Random Dev

Reputation: 52280

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

Upvotes: 5

John Hartsock
John Hartsock

Reputation: 86872

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)
{

}

Upvotes: 3

zapico
zapico

Reputation: 2396

It should be:

foreach(User user in userList)
{

}

Upvotes: 4

Donut
Donut

Reputation: 112815

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)
{

}

Upvotes: 9

Related Questions