2013-02-11 26 views
0

我有3名名单和类别:使用LINQ正确的测验,并按照主题

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 : Failed in question = 4 : Topic = "Listening" 

Id= 2 : Failed in question = 1 : Topic = "Verb to be" 

Id= 2 : Failed in question = 4 : Topic = "Listening"

主题百分比:

Listening = 2/4 = 50% incorrect 

Verb to be = 1/4 = 25% incorrect
+2

为什么要为'q1','q2'和'r1','r2'存储不同的属性?列表或数组不会更有意义吗?否则,我认为你必须通过反射来访问你的命名属性。 – Rup 2013-02-11 15:22:07

回答

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