c# - GroupBy and Count with condition using LINQ -
i have exam
table, , examresults
includes questions of specific exam. table contain question, answer selected, , answerpower indicate if answer correct or not:
- 1 (means correct),
- 0 (means wrong),
- null (means not answered , considered wrong).
i trying count
of wrong answers of specific exam grouped chapters.
the following give me count of answers grouped chapter, don't know set condition count wrong answers can answerpower!=1
var query = ex.examresults .groupby(p => new { p.question.chapter.chaptername, p.answerpower }) .select(g => new { g.key.chaptername, mistakes = g.count() });
inside count
can give condition below, note have removed answerpower
groupby
var query = ex.examresults .groupby(p => new { p.question.chapter.chaptername }) .select(g => new { g.key.chaptername, mistakes = g.count(x => x.answerpower!=1), correctanswers = g.count(x => x.answerpower==1) });
Comments
Post a Comment