The syntax seems mysterious. Please help here.

int? count = (from cartItems in storeDB.Carts
                          where cartItems.CartId == shoppingCartId
                          select (int?)cartItems.Count).Sum();
 
            return count ?? 0;

Thanks

Lets break it down: int? count declares a nullable integer variable named count. count is now a variable which can store either an integer value or NULL.

(from cartItems in storeDB.Carts
where cartItems.CartId == shoppingCartId
select (int?)cartItems.Count).Sum();

This is a LINQ query which returns the Count of the cartItems in a given Cart and sums them. return count ?? 0; Lastly the method returns a value. I am assuming that the method is declared with an 'int' return type and not 'int?' since they are converting a NULL value to 0. The syntax is a shorthand for something like:

if(count != null)
    return count;
else
    return 0;

Hope that helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.