Sesame
Sesame

Reputation: 3410

foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'

I'm trying to use Entity Framework to execute a stored procedure to populate a list but am running into issues because one of the stored proc parameters is an int.

Here's my code:

string param1 = "param1";
int param2 = 1;

List<MyEntity> myEntities = new List<MyEntity>();

using (MyEntities ctx = new MyEntities())
{
    foreach(MyEntity myEntity in ctx.FindMyEntity(param1, param2)    
    {
        myEntities.Add(myEntity);
    }
}

This gives me the following error:

foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'

I have a similar snippet of code that is almost identical, but the second param is a string. It works perfect and returns the anticipated results.

What can I do to make this work with an int param?

Upvotes: 3

Views: 4079

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062600

but am running into issues because one of the stored proc parameters is an int

No, you are running into issues because ctx.FindMyEntity(string,int) returns an int. And as the compiler quite rightly says: you can't foreach over that. Check that ctx.FindMyEntity is returning, vs what you think it should be returning.

Upvotes: 5

Related Questions