2017-01-18 79 views
0

我在數據庫表中的NullableBIGINT場。查詢空列Sql的Linq

我正在使用linqsql從c#中的表中檢索數據。

c.ExtensionID ==(的extensionID == 0(長)空:??的extensionID)

上面這段代碼是我在用的當前,但它不返回我的在ExtensionID列中有空的行。

但如果我使用它像c.ExtensionID == null那麼它將返回記錄。

以下是完整的LINQ to SQL代碼。

var q = (from c in dbContext.Investments 
     join cd in dbContext.ContractContractorDetails 
     on c.ContractContractorId equals cd.ID 
     join cc in dbContext.ContractorCategories 
     on c.ContractorCategory equals cc.ID 
     join ic in dbContext.InvestmentCategories 
     on c.InvestmentCategory equals ic.ID 
     where c.InvestmentClassificationType == type && cd.ContractId.Equals(ContractID) && c.Phase == phase && c.ExtensionID == (ExtensionId == 0 ? (long?)null : ExtensionId) 
     select new 
     { 
      ID = c.ID, 
      Year = c.Year, 
      ContractorCategory = c.ContractorCategory, 
      ContractorCategoryName = cc.Name, 
      CompanyName = c.CompanyName, 
      InvestmentCategory = c.InvestmentCategory, 
      InvestmentCategoryName = ic.Name, 
      Summary = c.Summary, 
      IsContractRelated = c.IsContractRelated, 
      InvestmentAmount = c.InvestmentAmount, 
      Phase = c.Phase, 
      InvestmentClassificationType = c.InvestmentClassificationType, 
      ContractContractorId = c.ContractContractorId, 
      CreatedBy = c.CreatedBy, 
      LastUpdatedOn = c.LastUpdatedOn, 
      LastUpdatedBy = c.LastUpdatedBy, 
      Period=c.Period, 
      ExtensionId = c.ExtensionID 
     }).ToList(); 
+0

的幫助也許''的extensionID從未'0'。你檢查'ExtensionId = 0'嗎? – Eldeniz

+0

是的,我檢查,它沒有返回任何行。 –

+0

你肯定有數據在數據庫'的extensionID = null'? – Eldeniz

回答

0

我能夠使用下面的代碼解決問題。

object.Equals(c.ExtensionID, (ExtensionId == 0 ? null : ExtensionId)) 

link