C# foreach 语句不能对“getenumerator”的公共定义类型的变量进行操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18462554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:16:29  来源:igfitidea点击:

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

c#asp.netado.net

提问by atifulrehmankhan khan

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'

编译器错误消息:CS1579:foreach 语句无法对“Task03Entities.Entites”类型的变量进行操作,因为“Task03Entities.Entites”不包含“GetEnumerator”的公共定义

采纳答案by DGibbs

It's simple: Task03Entities.Entitesis not a collection.

很简单:Task03Entities.Entites不是一个集合。

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...
}

回答by Tigran

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

考虑到 message 您的类型 Task03Entities.Entites不是集合和/或不可枚举类型。所以为了能够使用foreach它,你必须让它这样。

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

例如,可以查看:如何使 Visual C# 类在 foreach 语句中可用

回答by Tamizh venthan

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

只需在第一行添加 @model IEnumerable int 而不是常规的 @model 行