2010-04-27 62 views
0

我正在以下例外:左外连接被示出例外

空值不能被分配到与类型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 
     }; 
+1

请使用适当的日志记录来找出正在生成的SQL,然后编辑问题 - 这会使诊断变得更容易。 – 2010-04-27 06:16:13

回答

0

对于初学者来说,尝试用这种运行它:

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

您能得到什么?

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) 

注:行如果我在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