0

I have a Nullable BIGINT field in the database table.

I am using linq to sql to retrieve data from that table in c#.

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

The above piece of code is what i am using currently, but it does not return me the rows with null in ExtensionID Column.

but if i use it like c.ExtensionID == null then it will return records.

Below is the full linq to sql code.

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();
9
  • perhaps ExtensionId is never 0. Do you check ExtensionId =0 ? Commented Jan 18, 2017 at 5:26
  • yes i checked, it did not return me any rows. Commented Jan 18, 2017 at 5:31
  • Do you sure has data in database for ExtensionID=null ? Commented Jan 18, 2017 at 5:37
  • yes, ExtensionID has null values in the db Commented Jan 18, 2017 at 5:42
  • 1
    This senseless. Can you check c.ExtensionID == (ExtensionId == 0 ? null : ExtensionId)? Commented Jan 18, 2017 at 5:49

1 Answer 1

0

I was able to get the problem solved using the below code.

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

with the help of link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.