2010-04-27 92 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