查询Nullable Column Linq到Sql

时间:2017-01-18 05:16:56

标签: c# linq linq-to-sql

我在数据库表中有一个Nullable BIGINT字段。

我正在使用linqsql来检索c#中该表的数据。

  

c.ExtensionID ==(ExtensionId == 0?(long?)null:ExtensionId)

上面的代码是我目前正在使用的代码,但它不会返回ExtensionID列中带有null的行。

但如果我像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();

1 个答案:

答案 0 :(得分:0)

我能够使用以下代码解决问题。

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

link

的帮助下