0

我有 3 个列表和课程:

List<student_Answers> student_answer = new List<student_Answers>
{new student_Answers {id = "1", q1 ="*C", q2= "*D", q3 =  "*B", q4= "*A" },
new student_Answers {id = "2", q1 ="*D", q2= "*E", q3 =  "*B", q4= "*A" }};

List<answer> correct_answer = new List<answer> 
{new answer{r1 ="*C", r2= "*D", r3 =  "*B", r4= "*C" }};

List<Topic> topic_question = new List<Topic>
{ new Topic{ q1_topic ="Verb to be", q2_topic= "Verb to be", q3_topic =  "Listening", q4_topic= "Listening" }};

我试过了:

foreach (var na in student_answer)
    {var grade = from n in student_answer where !na.Contains(n) select n;}

它不起作用,我不知道如何将我的问题按主题分组。

预期输出

失败的问题:

Id= 1 : 失败的问题 = 4 : Topic = "Listening"

Id= 2 : 失败的问题 = 1 : Topic = "动词 to be"

Id= 2 : 失败的问题 = 4 : Topic = "Listening"

主题百分比:

听力 = 2/4 = 50% 不正确

动词 to be = 1/4 = 25% 不正确
4

1 回答 1

1

这里有一些代码可以为您指明正确的方向。

public class StudentQuestion : List<Question>
{
    public int StudentId { get; set; }

    public StudentQuestion(int studentId, IEnumerable<Question> questions)
    :base(questions)
    {
        StudentId = studentId;
    }

    public bool AddAnswer(int id, string response)
    {
        Question question = null;
        if((question = this.SingleOrDefault(q => q.Id == id)) == null)
            return false;

        question.Answer = response;
        return true;
    }

    public bool RemoveAnswer(int id)
    {
        Question question = null;
        if((question = this.SingleOrDefault(q => q.Id == id)) == null)
            return false;

        question.Answer = string.Empty;
        return true;
    }

    public double ScoreTest(IEnumerable<Answer> answers)
    {
        List<bool> score = this.Join(answers, a1 => a1.Answer.Response, a2 => a2.Response, (a1, a2) => a1.HasCorrectAnswer(a2)).ToList();
        return ((double)score.Where(s => s).Count()) / score.Count;
    }
}

public class Question
{
    public int Id { get; set; }
    public string Text { get; set; }
    public Answer Answer { get; set; }

    public bool HasCorrectAnswer(Answer correctAnswer)
    {
        return correctAnswer == Answer;
    }
}

public class Answer : IEquatable<Answer>
{
    public string Response { get; set; }

    public bool Equals(Answer answer)
    {
        if(answer == null) return false;

        return string.Compare(this.Response, answer.Response, true) == 0;
    }

    public override bool Equals(object obj)
    {
        if(obj == null)
            return false;

        var answerObj = obj as Answer;
        return answerObj == null ? false : Equals(answerObj);
    }

    public override int GetHashCode()
    {
        return Response.GetHashCode();
    }

    public static bool operator == (Answer answer1, Answer answer2)
    {
        if ((object)answer1 == null || ((object)answer2) == null)
            return object.Equals(answer1, answer2);

        return answer1.Equals(answer2);
    }

    public static bool operator != (Answer answer1, Answer answer2)
    {
        if ((object)answer1 == null || ((object)answer2) == null)
            return ! object.Equals(answer1, answer2);

        return ! (answer1.Equals(answer2));
    }
}
于 2013-02-11T16:47:54.503 回答