Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach statement cannot operate on variables of type public definition for 'getenumerator'

Task03Entities.Entites entities = new Task03Entities.Entites();

// Creat a object for my entites class
Task03BAL.BAL bal = new Task03BAL.BAL();

// creat a object of BAL to call Get Data Method
List<Task03Entities.Entites> entitiesList = new List<Task03Entities.Entites>();

// make a list of class Entities
entitiesList = bal.GetData(entities);

// store data in list
ViewState.Add("Products", entitiesList.ToArray());

// use view state to store entitieslist
Task03Entities.Entites[] entitiesArray =(Task03Entities.Entites[])ViewState["Products"];
List<Task03Entities.Entites> ViewStateList =new List<Task03Entities.Entites(entitiesArray);

// so now ViewStateList contain entitieslist data
// Now the Problem which i am facing is 
if (Request.QueryString["count"] != null) // this is okay
{
  listCount =Convert.ToInt32(Request.QueryString["count"]);   
}

for (int i = (listCount * 2)-2; i < listCount * 2; i++)
{
  Task03Entities.Entites newViewStateList = new Task03Entities.Entites();
  newViewStateList = ViewStateList[i];

// view state contain a list of data here on above line m trying to acess single data
%>
<table>
<%                  

foreach (Task03Entities.Entites item in newViewStateList)
// on above line i am getting following error message 

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'Task03Entities.Entites' because 'Task03Entities.Entites' does not contain a public definition for 'GetEnumerator'

like image 589
atifulrehmankhan khan Avatar asked Aug 27 '13 10:08

atifulrehmankhan khan


3 Answers

It's simple: Task03Entities.Entites is not a collection.

You initialize an object here:

Task03Entities.Entites newViewStateList = new Task03Entities.Entites();

And then attempt to iterate over it as if it were a collection:

foreach (Task03Entities.Entites item in newViewStateList)
{

}

I suspect you want something like:

List<Task03Entities.Entites> newViewStateList = new List<Task03Entities.Entites>();

foreach (Task03Entities.Entites item in newViewStateList)
{
    //code...
}
like image 51
DGibbs Avatar answered Nov 03 '22 12:11

DGibbs


just add @model IEnumerable int the first line instead of your regular @model line

like image 44
Tamizh venthan Avatar answered Nov 03 '22 14:11

Tamizh venthan


Considering message your type Task03Entities.Entites is not a collection and/or not enumerable type. So in order to be able to use foreach on it, you have to make it such.

For example can have a look on: How to make a Visual C# class usable in a foreach statement

like image 30
Tigran Avatar answered Nov 03 '22 13:11

Tigran