我有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:主题="听力"失败Id=2:问题=1中失败:主题="待动词"Id=2:问题=4失败:主题="听力"
主题百分比:
听力=2/4=50%错误待动词=1/4=25%不正确
这里有一些代码可以为您指明正确的方向。
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));
}
}