C# 操作员 '??' 不能应用于“int”和“int”类型的操作数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17174068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:45:51  来源:igfitidea点击:

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

c#linq

提问by Shantanu Sen

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

我有这个 beloq 查询,它给了我一个例外。现在 j.job_quote.JobQuoteID 是一个身份列,它为空,周四尝试创建一个检查是否为空然后标识为 0。请帮助我为什么会遇到这个问题,我可以做些什么来处理它

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();

采纳答案by Zbigniew

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

??运算符左侧的变量必须可以为空(这意味着您可以为其分配空值),在您的情况下JobQuoteID应该是类型int?notint

回答by Servy

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

编译器告诉你它j.job_quote.JobQuoteID的类型是int。Anint不能为 null,因为它是不可为 null 的值类型。该??运营商无法在类型不能为空被调用。

回答by evanmcdonnal

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 intit would work. The operator is binary and works like so; a ?? bsays that if ais 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 ?? 4assuming a, b, and c are all nullable ints, it will take the first non null value.

那是空合并运算符,它仅适用于可空类型,或者更确切地说,左侧必须是可空类型(我的语言在那里可能是错误的,但是当我说可空时,我的意思是 allNullable<T>和引用类型)。如果你有int?而不是int它会起作用。运算符是二元的,工作原理是这样的;a ?? b说如果a为空,则使用 b 作为值。您可以根据需要多次链接它。所以我可以int willNeverBeNull = a ?? b ?? c ?? 4假设 a、b 和 c 都是可为空的整数,它将采用第一个非空值。

回答by Kundan Singh Chouhan

As @Servy said issue is not with the JobQuoteIDinstead I think the error is occurring because your job_quoteobject is null, thus the expression should be:

正如@Servy 所说的问题不在于JobQuoteID相反,我认为发生错误是因为您的job_quote对象为空,因此表达式应该是:

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

Hope this will fix your issue.

希望这会解决您的问题。