LINQ查询返回的结果比整个数据库中的结果更多

jam*_*one 0 c# linq linq-to-sql

我有以下LINQ查询.问题是,当tblSurveys总共只有20个时,它会返回13k的结果.我究竟做错了什么?

from s in surveyContext.tblSurveys
from st in surveyContext.tblTypes_for_Surveys
from t in surveyContext.tblSurvey_Types
where (s.Survey_Date >= startDate && s.Survey_Date <= stopDate) && 
      (s.Unsubstantiated || 
         (st.SurveyID == s.SurveyID && st.SurveyTypeID == t.SurveyTypeID && 
         t.UnsubstantiatedAvailable && (from d in surveyContext.tblDeficiencies
                                         where d.SurveyID == s.SurveyID
                                        select d.DeficiencyID).Count() == 0))
orderby s.Survey_Date
select s;
Run Code Online (Sandbox Code Playgroud)

Fre*_*dou 5

我可以在那里看到一个交叉连接,看看<-------

           from s in surveyContext.tblSurveys
           from st in surveyContext.tblTypes_for_Surveys
           from t in surveyContext.tblSurvey_Types
           where (s.Survey_Date >= startDate && s.Survey_Date <= stopDate) && 
                 (s.Unsubstantiated || <-------
                    (st.SurveyID == s.SurveyID && st.SurveyTypeID == t.SurveyTypeID && 
                    t.UnsubstantiatedAvailable && (from d in surveyContext.tblDeficiencies
                                                   where d.SurveyID == s.SurveyID
                                                   select d.DeficiencyID).Count() == 0))
           orderby s.Survey_Date
           select s;
Run Code Online (Sandbox Code Playgroud)

看来你需要在这里做左联

  • 交叉连接将生成三组的笛卡尔积(因此您的13k行而不是20行).您应该使用显式连接语句或在where子句中设置连接条件. (6认同)
  • 交叉连接,如果你有tblSurveys 20纪录和30 tblTypes_for_Surveys然后50 tblSurvey_Types,这将返回30000行 (3认同)