简体   繁体   English

访问另一个功能的实体查询结果

[英]access entity query result from another func

hi i have this query in func 1 and i want access to this query from func 2 how i can do this? 嗨,我在功能1中有此查询,我想从功能2中访问此查询,我该怎么做?

func 1: 功能1:

var data = db.Question.Where(x => x.GroupId == id)
                      .OrderBy(x => Guid.NewGuid())
                      .Take(Convert.ToInt32(txtTedad.Text));  

func 2: 功能2:

if (answerlist.Items[i].ToString() == data.AsEnumerable().ElementAt(index).Id.ToString())
{
    sahih = sahih + 1;
}

can i create a global variable and assign query result to my variable? 如何创建全局变量并将查询结果分配给我的变量? something like this: 像这样的东西:

VarType mydata;

func1(){
    mydata = query;
}

func2(){
    mydata.AsEnumerable().ElementAt(index).Id.ToString()
}

I don't know if I understood correctly what you were trying to achieve, but I think it is something like the code below. 我不知道我是否正确理解您要实现的目标,但是我认为这类似于下面的代码。 The code and the design can be greatly simplified and/or improved, but this would be a step in the right direction. 代码和设计可以大大简化和/或改进,但这将是朝着正确方向迈出的一步。 This is merely an example to illustrate how you can achieve what you described you wanted. 这仅是一个示例,说明您如何实现自己想要的内容。

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;
}

In short, you do not need to PASS a function to another for what you need, you simply call one function from the other! 简而言之,您无需根据需要将一个函数传递给另一个函数,只需从另一个函数中调用一个函数即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM