嗨我在func 1中有这个查询,我希望从func 2访问这个查询我怎么能这样做?
func 1:
var data = db.Question.Where(x => x.GroupId == id)
.OrderBy(x => Guid.NewGuid())
.Take(Convert.ToInt32(txtTedad.Text));
func 2:
if (answerlist.Items[i].ToString() == data.AsEnumerable().ElementAt(index).Id.ToString())
{
sahih = sahih + 1;
}
我可以创建一个全局变量并将查询结果分配给我的变量吗?像这样的东西:
VarType mydata;
func1(){
mydata = query;
}
func2(){
mydata.AsEnumerable().ElementAt(index).Id.ToString()
}
答案 0 :(得分:0)
我不知道我是否理解你想要实现的目标,但我认为它类似于下面的代码。代码和设计可以大大简化和/或改进,但这将是朝着正确方向迈出的一步。这只是一个例子,用于说明如何实现您想要的目标。
private IQueryable<Question> getQuestionAnswers(int questionCount, int studentID)
{
return db.Question.Where(x => x.GroupId == studentId)
.OrderBy(x => Guid.NewGuid())
.Take(questionCount);
}
private int calculateStudentScore(int studentId, .... answerList)
{
int studentScore = 0;
var studentAnswers = getQuestionAnswers(answerList.Length, studentId).ToList();
//research IEnumerable.Zip for more fun ways to calculate the score (zip required answers and student answers and just sum over the resulting list where answers are equal to obtain the score
for (int i = 0; i < answerList.Length; i++)
{
//if (answerlist check against studentAnswers)
{
studentscore++;
}
}
return studentScore;
}
简而言之,您不需要将功能传递给另一个功能,只需从另一个功能中调用一个功能!