2013-02-11 23 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)); 
    } 
}