gpt4 book ai didi

c# - foreach 语句不能对 'getenumerator' 的公共(public)定义类型的变量进行操作

转载 作者:太空狗 更新时间:2023-10-29 17:40:19 25 4
gpt4 key购买 nike

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'

最佳答案

很简单: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...
}

关于c# - foreach 语句不能对 'getenumerator' 的公共(public)定义类型的变量进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com