18

I have this beloq query which is giving me an exception. Now j.job_quote.JobQuoteID is an identity column which null, thurs trying to create a check if null then identify as 0. Please help why i am getting the issue and what can i do to handle it

var Qryjob = (from j in at.jobs 

                     where j.JobID == Convert.ToInt32(jobid)
                     select new {
                         JobID = j.JobID,
                         InsertedDate = j.InsertedDate,
                         FirstName = j.user.FirstName,
                         LastName = j.user.LastName,
                         City = j.user.City,
                         ServiceName = j.service.ServiceName,
                         ServiceTypeName = j.service_type.ServiceTypeName,
                         BudgetName = j.budget.BudgetName,
                         IsApproved = j.IsApproved,
                         IsAssigned = j.IsAssigned,
                         IsQuoted = j.IsQuoted,
                         job_notif_price = j.job_notif_price,
                         Description = j.Description,
                         PaymentTypeName = j.payment_type.PaymentTypeName,
                         DuePeriodName = j.due_period.DuePeriodName,
                         QuoteStatus = j.job_quote.QuoteStatus,
                         JobStatus = j.job_quote.JobStatus,
                         comments = j.job_quote.comments,
                         IsPartnerApply = j.job_quote.IsPartnerApply,
                         Applycomment = j.job_quote.ApplyComments,
                         JobQuoteID = j.job_quote.JobQuoteID ?? 0
                     }).ToList();

4 Answers 4

58

The variable on the left side of ?? operator has to be nullable (which means that you can assign null to it), in your case JobQuoteID should be of type int? not int

12

The compiler is telling you that j.job_quote.JobQuoteID is of type int. An int cannot be null, as it is a non-nullable value type. The ?? operator cannot be called on a type that is not nullable.

7

That is the null-coalescing operator, it only applies to nullable types, or rather the left hand side must be a nullable type (my language might be wrong there but when I say nullable i mean all Nullable<T>'s and reference types). If you had int? instead of int it would work. The operator is binary and works like so; a ?? b says that if a is null then use b for the value. You can chain this as many times as you'd like. So I could do int willNeverBeNull = a ?? b ?? c ?? 4 assuming a, b, and c are all nullable ints, it will take the first non null value.

1

As @Servy said issue is not with the JobQuoteID instead I think the error is occurring because your job_quote object is null, thus the expression should be:

JobQuoteID = (j.job_quote ?? new job_quote { JobQuoteID = 0 }).JobQuoteID

Hope this will fix your issue.

1
  • This is simple and original!
    – S.H.
    Apr 11, 2022 at 7:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.