Explanation - MVC

Posted on 2012-09-09
Last Modified: 2012-09-10

I am following the Microsoft tutorial to create a music store. Link :  

I'm at the point where they are creating the shopping cart, and on the shopping cart class they have the following code:

public void AddToCart(Album album)
            //Get the matching cart and album instances
            var cartItem = storeDB.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId
                    && c.AlbumId == album.AlbumId);

            if (cartItem == null)
                //Create a new cart item if no cart item exists
                cartItem = new Cart
                    AlbumId = album.AlbumId,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                //if the item does not exist in the cart then add one to the quantity
            //Save changes

Can anyone explain this to me?  Especially the "SingleOrDefault" part.
Question by:Jasmin01
    LVL 49

    Accepted Solution

    Does this help

    In this context what it is saying is that you want a cart item matching the criteria to be returned.

    There should only be one (otherwise you have multiple carts matching the same criteria which is no good). If more than one is found it will throw an exception to tell you something is wrong.

    If only one is found that item is returned.

    If none is found then the default value for the type is returned.
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    If none is found then the default value for the type is returned.
    ...which is always null in the case of classes  ; )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now