Linq, how to check a null value and use the value 0 instead of zero?

I have the following query:

var ordersPlacedBeforeOneHourList = ordersPlacedBeforeOneHourAgo.Select(order => order.Promo.PercentOff * (double) order.TotalCost).ToList(); 

Some of my order.Promo.PercentOff are null. How to change the above line so that if order.Promo is null, it behaves as if the values ​​are 0 ?

The idea is that if a specific advertising campaign is not applied, I will calculate the promotion cost as 0 * order.TotalCost (which will always be 0) and go to the following values ​​to calculate where orders.Promo may not be zero , but by line.

+4
source share
1 answer

The null-coalescing statement is your friend.

 (order.Promo.PercentOff ?? 0) 
+13
source

Source: https://habr.com/ru/post/1435276/


All Articles