LINQ中的左外连接显示异常

时间:2010-04-27 06:01:05

标签: c# linq

我收到以下异常:

  

无法将null值分配给System.Int32类型的成员   可空值类型。

下面是我的LINQ语句,其中QuestionId是我表中的主键:

var questionViewsData =
      from questionViews in objDc.SC_QuestionsViews
      join questions in objDc.SC_Questions
      on questionViews.QuestionId equals questions.QuestionId into qs
      from questions in qs.DefaultIfEmpty()
      where questionViews.CreatedDate.Date == new DateTime(2010, 4,27)
      select new 
      {
          Selected =(questions == null ?-1:questions.QuestionId),
          QuestioinTitle = questions.Title,
          VotesCount = questions.VotesCount 
      };

2 个答案:

答案 0 :(得分:1)

更改行

where questionViews.CreatedDate.Date == new DateTime(2010, 4,27)

where questionViews.CreatedDate == new DateTime(2010, 4, 27)

作为您可以运行的测试

where questionViews.CreatedDate > new DateTime(2010, 4, 26)

N.B。如果我在LinqPad c#语句中包含.Date,则会收到以下错误消息并删除.Date允许该语句运行。

System.Nullable<System.DateTime>' does not contain a definition for 'Date' and no
extension method 'Date' accepting a first argument of type
'System.Nullable<System.DateTime>' could be found

答案 1 :(得分:0)

对于初学者,请尝试使用它运行:

select new 
      {
          Selected = questions.QuestionId,
          QuestioinTitle = questions.Title,
          VotesCount = questions.VotesCount 
      };

你得到了什么?