使用linq查询无法在组中获取输出
我的桌子是:使用linq查询无法在组中获取输出,linq,Linq,我的桌子是: Student_answer_Master: Stud_ans_id QuestionId OptionId iscorrect 1 25 1 1 2 26 1 0 3 27 2 1 4
Student_answer_Master:
Stud_ans_id QuestionId OptionId iscorrect
1 25 1 1
2 26 1 0
3 27 2 1
4 27 3 1
5 27 4 1
6 28 1 1
8 29 1 0
9 29 2 1
10 29 3 1
(我的选项有单选和多选(复选框))
现在我只想得到正确的问题的总数
1-表示正确
0表示不正确
我想要输出:3(对于问题ID 25,27,28)
这是我的linq查询:
var data = (from temp in context.Student_Answer_Master
where temp.isCorrect == '1'
group temp by temp.Question_Id into g
select new {g.Key}).Count();
但是这里的计数是0
有人能告诉我我的查询有什么问题吗?试试这个,应该可以
var data = (from temp in s
group temp by temp.Question_Id into g
select new { questionid = g.Key, minIscorrect = g.Min(x => x.isCorrect) }).Where(y => y.minIscorrect != 0).Count();
在您的查询中,有两件事情看起来不对
- 将int与char temp.isCorrect==1进行比较,如果将其设置为temp.isCorrect==1,则仍然会得到4作为计数,因为您正在删除问题id为29的答案错误的行
- 一旦你进入小组,你需要检查这个问题是否有错误的答案 如果是,则将其消除,然后进行计数