Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - Adding 2 decimals with one possibly being null

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to worry about null values. Using the null-coalescing operator ?? you can get a 0 when the query result is null.

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

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

    return total + total2;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...