Как обрабатывать нулевое значение ⇐ C#
-
Anonymous
Как обрабатывать нулевое значение
I have the following if statement:
if ((product.EmailType.Contains("cafe", StringComparison.OrdinalIgnoreCase) || product.EmailType.Contains("vendor", StringComparison.OrdinalIgnoreCase)) && product.Supplier.Supplier_Type.Contains("Support")) { // Do some stuff } Basically if EmailType contains a specific value or a different specific value, and the SupplierType is "Support", do something.
However, it is very common for the Supplier_Type to be null. With how it is above it throws an exception.
How do I account for it possibly being null? If it's null it should evaluate the whole if statement as false and move on.
I tried null coalescing on the final line but I get an error
Operator ?? cannot be applied to type bool and bool
if ((product.EmailType.Contains("cafe", StringComparison.OrdinalIgnoreCase) || product.EmailType.Contains("vendor", StringComparison.OrdinalIgnoreCase)) && (product.Supplier.Supplier_Type.Contains("Support") ?? false)) { // Do some stuff } I also tried null handling by writing the final line as the following, but get the same error:
&& product.Supplier.Supplier_Type?.Contains("Support")
Источник: https://stackoverflow.com/questions/781 ... null-value
I have the following if statement:
if ((product.EmailType.Contains("cafe", StringComparison.OrdinalIgnoreCase) || product.EmailType.Contains("vendor", StringComparison.OrdinalIgnoreCase)) && product.Supplier.Supplier_Type.Contains("Support")) { // Do some stuff } Basically if EmailType contains a specific value or a different specific value, and the SupplierType is "Support", do something.
However, it is very common for the Supplier_Type to be null. With how it is above it throws an exception.
How do I account for it possibly being null? If it's null it should evaluate the whole if statement as false and move on.
I tried null coalescing on the final line but I get an error
Operator ?? cannot be applied to type bool and bool
if ((product.EmailType.Contains("cafe", StringComparison.OrdinalIgnoreCase) || product.EmailType.Contains("vendor", StringComparison.OrdinalIgnoreCase)) && (product.Supplier.Supplier_Type.Contains("Support") ?? false)) { // Do some stuff } I also tried null handling by writing the final line as the following, but get the same error:
&& product.Supplier.Supplier_Type?.Contains("Support")
Источник: https://stackoverflow.com/questions/781 ... null-value
Мобильная версия