LINQ在写入SQL返回结果时在同一查询中返回Null

时间:2018-12-03 07:24:12

标签: .net sql-server entity-framework linq linq-to-entities

我有两个表Survey_ResponseQuestions

Survey_Response中的列是:

ID PK
SurveyID FK
QuestionID FK
Response varchar
FilledBy FK

Questions中的列是:

ID PK
Text varchar
QuestionType varchar
Options varchar

SQL Server查询工作正常:

select * 
from Survey_Response SR
inner join Questions q on SR.QuestionID = q.ID
where SR.SurveyID='1008'

返回空值的LINQ是

    ResponseList = (from r in ObjectSur.Survey_Response
                    join q in ObjectSur.Questions on r.SurveyID equals q.ID
                    where r.SurveyID.Equals(prefix)
                    select new ResponseViewModel
                    {
                        QuestionID = r.QuestionID,
                        Text = q.Text,
                        Response = r.Response,
                        FilledBy = r.FilledBy
                    }).ToList();

我认为两个查询都是相同的。

希望得到您的建议。

谢谢

1 个答案:

答案 0 :(得分:2)

这两个查询与您在SQL QuestionID和Linq SurveyID

上的 join 查询不同
ResponseList = (from r in ObjectSur.Survey_Response
                join q in ObjectSur.Questions on r.QuestionID equals q.ID //<-- change here
                where r.SurveyID.Equals(prefix)
                select new ResponseViewModel
                {
                    QuestionID = r.QuestionID,
                    Text = q.Text,
                    Response = r.Response,
                    FilledBy = r.FilledBy
                }).ToList();