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

本文关键字:变量 类型 操作 定义 语句 不能 getenumerator foreach | 更新日期: 2023-09-27 18:29:20

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 

编译器错误消息: CS1579: foreach 语句无法对 类型为"Task03Entities.Entites"的变量,因为 "Task03Entities.Entites"不包含以下公共定义: "获取枚举器">

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

很简单

Task03Entities.Entites不是一个集合。

您在此处初始化一个对象:

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

然后尝试像集合一样迭代它:

foreach (Task03Entities.Entites item in newViewStateList)
{
}

我怀疑你想要这样的东西:

List<Task03Entities.Entites> newViewStateList = new List<Task03Entities.Entites>();
foreach (Task03Entities.Entites item in newViewStateList)
{
    //code...
}

考虑到消息,您的类型Task03Entities.Entites不是集合和/或不可枚举类型。因此,为了能够在其上使用foreach,您必须使其如此。

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

只需添加@model IEnumerableint 第一行而不是常规@model行