I am getting a total on my cart items and one may possibly be null. This has come about because I am pulling a price from the database according to the class of the Part. Before I just added everything in the cart according to one price column. Now I am in need of doing so by the class of the part.
[UPDATE] - I explained this a little better..
There might be parts in the cart of one type of class (Class being the name I gave the column in the data table) If the there are items in the cart and they do not meet both "conditions" I will still need a total. This is the code I have right now.
public decimal GetTotal()
{
decimal? total = (from cartItems in storeDB.Carts
where ((cartItems.CartId == ShoppingCartId) && (cartItems.Class != "MACH") || ( cartItems.Class != "PRT"))
select (int?)cartItems.Count * cartItems.EParts.SellingPrice).Sum();
decimal? total2 = (from cartItems in storeDB.Carts
where ((cartItems.CartId == ShoppingCartId) && (cartItems.Class == "MACH") || (cartItems.Class == "PRT"))
select (int?)cartItems.Count * cartItems.EParts.MachinedPrice).Sum();
decimal? totals = total + total2;
return totals ?? decimal.Zero;
}
Keeping in mind that the Values of prices are pulled from another table and not held in the Cart table itself.
As of the current code it will not "Total" because there are no parts in the cart that meet criteria Total2
and it returns NULL..
I am open to suggestions to changing the code on how I am getting it or if there is something I can add to the Totals addition to not care if on condition is null. Or maybe returning 0.00
if null.
Thanks for your help!
[UPDATE]
The Conditions that take place in this code may or may not return a NULL decimal. If there are no Items in the Cart that meet total
but do meet total2
then a null condition is reached - Hence the reason for ?? 0
Which I realized after I posted the question. I am working with a very large database and try not to repeat data as that is a no-no in databases. So that is the reason for not adding the Price to the Cart Table. It is an unnecessary step. These parts are not all "marked-up" the same. So I had to implement a way to separate them for the different up charges and did not want to hardcode them into the pages. Easier to do in the database. Hope this helps and clears up any misunderstandings..
question from:
https://stackoverflow.com/questions/65924299/adding-2-decimals-with-one-possibly-being-null 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…