C# 使用LINQ的GroupBy和带条件计数
我有一个C# 使用LINQ的GroupBy和带条件计数,c#,linq,group-by,C#,Linq,Group By,我有一个考试表格,然后是考试结果,其中包括特定考试的所有问题。此表将包含问题、所选答案和回答能力,以表明答案是否正确: 1(表示正确) 0(表示错误) NULL(表示未应答,将被视为错误) 我正试图得到一个特定考试的错误答案的计数,按章节分组 下面将给我按章节分组的答案计数,但我不知道在哪里确切地设置条件以仅计数错误答案,这可以是AnswerPower=1 var query = ex.ExamResults .GroupBy(
考试
表格,然后是考试结果
,其中包括特定考试的所有问题。此表将包含问题、所选答案和回答能力,以表明答案是否正确:
- 1(表示正确)
- 0(表示错误)
- NULL(表示未应答,将被视为错误)
计数
,按章节分组
下面将给我按章节分组的答案计数,但我不知道在哪里确切地设置条件以仅计数错误答案,这可以是AnswerPower=1
var query = ex.ExamResults
.GroupBy(p => new
{
p.Question.Chapter.ChapterName,
p.AnswerPower
})
.Select(g => new
{
g.Key.ChapterName,
Mistakes = g.Count()
});
在
count
中,您可以给出如下条件,请注意,我已从GroupBy
中删除了AnswerPower
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)
});
在
count
中,您可以给出如下条件,请注意,我已从GroupBy
中删除了AnswerPower
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)
});
在
Where
中没有直接的p.AnswerPower
,我想你的意思是p.Key.AnswerPower=1
Where中没有直接的p.AnswerPower
,我想你指的是p.Key.AnswerPower=1