【问题标题】:LINQ returning Null while same query when write in SQL return resultLINQ 在写入 SQL 返回结果时返回 Null 而相同的查询
【发布时间】:2018-12-03 07:24:12
【问题描述】:

我有两张桌子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();

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

希望得到您的建议。

谢谢

【问题讨论】:

  • 它返回空列表还是空列表? prefix 和工作查询中的一样吗?
  • 另外你加入 QuestionID 在 SQL 然后 SurveyID 在 linq
  • 显示“ResponseList count 0”并且前缀是相同的值
  • :/我是个盲人!

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


【解决方案1】:

这两个查询与您 加入 SQL 中的 QuestionID 然后 Linq 中的 SurveyID 不同

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();

【讨论】:

    猜你喜欢
    • 2016-09-30
    • 2018-11-28
    • 2011-06-24
    • 2010-12-07
    • 2016-11-18
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2021-05-29
    相关资源
    最近更新 更多