从另一个函数访问实体查询结果
access entity query result from another func
嗨,我在 func 1 中有这个查询,我想从 func 2 访问这个查询,我该怎么做?
函数 1:
var data = db.Question.Where(x => x.GroupId == id)
.OrderBy(x => Guid.NewGuid())
.Take(Convert.ToInt32(txtTedad.Text));
函数 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()
}
我不知道我是否理解正确你想要达到的目的,但我认为它类似于下面的代码。代码和设计可以大大简化 and/or 改进,但这将是朝着正确方向迈出的一步。这只是一个示例,用于说明如何实现您所描述的目标。
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;
}
简而言之,您不需要将一个函数传递给另一个函数来满足您的需要,您只需从另一个函数中调用一个函数即可!
嗨,我在 func 1 中有这个查询,我想从 func 2 访问这个查询,我该怎么做?
函数 1:
var data = db.Question.Where(x => x.GroupId == id)
.OrderBy(x => Guid.NewGuid())
.Take(Convert.ToInt32(txtTedad.Text));
函数 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()
}
我不知道我是否理解正确你想要达到的目的,但我认为它类似于下面的代码。代码和设计可以大大简化 and/or 改进,但这将是朝着正确方向迈出的一步。这只是一个示例,用于说明如何实现您所描述的目标。
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;
}
简而言之,您不需要将一个函数传递给另一个函数来满足您的需要,您只需从另一个函数中调用一个函数即可!