查看这个,
from qg in this.Question_Group
Where qg.Question_Group_Question.Where(qgq => qgq.Question.Removed_Flag == false).Count() > 0
& qg.Question_Group_Question.Any(qgq => qgq.Question.Removed_Flag == false)
Select qg
如果集合 (Question_Group_Question) 中的 ANY 项 (Question) 具有具有特定值 (false) 的成员 (Removed_Flag),则您可以看到 qg.Question_Group_Question.Any(qgq => qgq.Question.Removed_Flag == false) 正在返回 true。
如果您打算查询/获取 Question_Group 实体,其中 ALL 的 Removed_Flag 为 false,则 Question_Group_Question 集合中的 Question 实体。
from qg in this.Question_Group
Where qg.Question_Group_Question.Where(qgq => qgq.Question.Removed_Flag == false).Count() > 0 & qg.Question_Group_Question.All(qgq => qgq.Question.Removed_Flag == false)
Select qg
根据我对您当前查询的了解,您实际上是在执行内部联接,因此在 Question_Group 与 Question_Group_Question 的关系中(即 1:N),您将返回 @ 的实体987654331@ 对应每个Question_Group_Question。
这似乎是您打算做的,所以为什么不直接在Question_Group_Question 上查询:(对于 Question_Group_Question 到 Question_Group 是 N:1)
from qgq in this.Question_Group_Question
where qgq.Question.Removed_Flag == false
select qgq.Question_Group
另外(和/或另外),我建议您在查询中使用使用 DTO。特别是如果您想明确保留 1:N 结构。
class QuestionGroupQuestionPair {
public Question_Group QuestionGroup {get;set;}
public IEnumerable<Question_Group_Question> QuestionCollection {get;set;}
}
IEnumerable<QuestionGroupQuestionPair> query = from qg in this.Question_Group
select new QuestionGroupQuestion()
{
QuestionGroup = qg,
QuestionCollection = qg.Question_Group_Question.Where(qgq => qgq.Question.Removed_Flag == false)
}
QuestionGroupQuestion 类成为查询表达式的一部分,因此在执行的生成的 SQL 语句中表达。