Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '??' cannot be applied to operands of type 'int' and 'int' [closed]

Tags:

c#

linq

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();
like image 566
Shantanu Sen Avatar asked Jun 18 '13 16:06

Shantanu Sen


3 Answers

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

like image 199
Zbigniew Avatar answered Oct 22 '22 23:10

Zbigniew


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.

like image 27
Servy Avatar answered Oct 22 '22 22:10

Servy


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.

like image 6
evanmcdonnal Avatar answered Oct 22 '22 22:10

evanmcdonnal