Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检索所有viewstate列表_C#_Asp.net_Viewstate - Fatal编程技术网

C# 如何检索所有viewstate列表

C# 如何检索所有viewstate列表,c#,asp.net,viewstate,C#,Asp.net,Viewstate,我正在与几个学生保存这样的视图状态: ViewState[currentStudent] = currentGradesList; 但是现在我需要获取所有的ViewState来获得所有等级的平均值,我已经学会了用字符串这样做: foreach(ViewState.Keys中的stringstr){..}这样就行了 但现在当我尝试 foreach(List<double> grades in ViewState.Keys) {....} 我猜它说键是字符串,但是我怎么才能得到所有

我正在与几个学生保存这样的视图状态:

 ViewState[currentStudent] = currentGradesList;
但是现在我需要获取所有的ViewState来获得所有等级的平均值,我已经学会了用字符串这样做:

foreach(ViewState.Keys中的stringstr){..}
这样就行了

但现在当我尝试

foreach(List<double> grades in ViewState.Keys) {....}

我猜它说键是字符串,但是我怎么才能得到所有的列表呢。使用foreach循环变量的值(现在是ViewState字典中键的名称)从ViewState获取值

将foreach循环更改为如下内容

foreach(var key in ViewState.keys)[
    var grades = ViewState[key] as List<double>;
    //LINQ has built in Average and Sum abilities on lists
    //I don't know what a CurrentStudentGrades looks like
    //but here is an example of using the built in average
    var studentAverage = grades.Average(x=>x.Grade);
    //do whatever else you are wanting to do
}
foreach(ViewState.keys中的var键)[
var等级=查看状态[键]作为列表;
//LINQ在列表中内置了平均和求和能力
//我不知道现在的学生是什么样子
//但这里有一个使用内置平均值的示例
var studentAverage=成绩.平均值(x=>x.成绩);
//做你想做的任何事情
}

您正在遍历
foreach
循环中键的名称,而不是实际值。您可以使用foreach循环变量的值(现在是ViewState字典中键的名称)从ViewState获取值

将foreach循环更改为如下内容

foreach(var key in ViewState.keys)[
    var grades = ViewState[key] as List<double>;
    //LINQ has built in Average and Sum abilities on lists
    //I don't know what a CurrentStudentGrades looks like
    //but here is an example of using the built in average
    var studentAverage = grades.Average(x=>x.Grade);
    //do whatever else you are wanting to do
}
foreach(ViewState.keys中的var键)[
var等级=查看状态[键]作为列表;
//LINQ在列表中内置了平均和求和能力
//我不知道现在的学生是什么样子
//但这里有一个使用内置平均值的示例
var studentAverage=成绩.平均值(x=>x.成绩);
//做你想做的任何事情
}
foreach(ViewState.Keys中的字符串str)
{
var等级=查看状态[str]作为列表;
如果(等级!=null)
{
var average=等级。average();
}
}
foreach(ViewState.Keys中的字符串str)
{
var等级=查看状态[str]作为列表;
如果(等级!=null)
{
var average=等级。average();
}
}
解决方案: 我在循环浏览viewstate键的名称,而不是值。所以现在我将其更改为

Foreach (string str in ViewState.Keys) 
{ //And then the value of "str"...
 List<double> templist = (List<double>)ViewState[str];
解决方案: 我在循环浏览viewstate键的名称,而不是值。所以现在我将其更改为

Foreach (string str in ViewState.Keys) 
{ //And then the value of "str"...
 List<double> templist = (List<double>)ViewState[str];

您不应该出于这些目的使用ViewState,这些值是如何进入ViewState的?您可以从任何源(如DB/file)获取它们吗?您不应该出于这些目的使用ViewState,这些值是如何进入ViewState的?您可以从任何源(如DB/file)获取它们吗?谢谢朋友,您是对的,我混合了名称和值。我从未使用过var或作为,所以我改为这样做:)foreach(ViewState.Keys中的字符串str){List templast=(List)ViewState[str];}现在我正在等待SOF允许我接受答案,你们太快了!:DaLinq解决方案很好而且简单,但我正在准备考试,他们希望我不要使用it@humudu-棒极了,伙计,很高兴你让它再次工作。我了解LINQ,知道如何自己做很好。哦,var只是告诉编译器“impl”icitly'确定类型。它将查看var声明并说,嘿,你给我分配了一个字符串作为值,我必须是一个字符串变量!谢谢你的朋友,你是对的,我混合了名称和值。我从未使用过var或as,所以我改为这样做:)foreach(ViewState.Keys中的字符串str){List templast=(List)ViewState[str];}现在我正在等待SOF允许我接受答案,你们太快了!:DaLinq解决方案很好而且简单,但我正在准备考试,他们希望我不要使用it@humudu-棒极了,伙计,很高兴你让它再次工作。我了解LINQ,知道如何自己做很好。哦,var只是告诉编译器“impl”icitly'确定类型。它将查看var声明并说,嘿,你给我分配了一个字符串作为值,我必须是一个字符串变量!谢谢!快速回答请解释你的答案,以便将来的其他人知道为什么这样做。谢谢!快速回答请解释你的答案,以便将来的其他人知道为什么这样做这是工作。
foreach (double grade in templist)
{
currenttotal += grade;
}
currentaverage = currenttotal / templist.count;
AllAveragesList.add(currentaverage)

foreach (double average in AllAveragesList)
{
totalOfAll += average
}
averageOfAll = totalOfAll / AllAveragesList.count();`