Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ的GroupBy和带条件计数_C#_Linq_Group By - Fatal编程技术网

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