0

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

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Geekitygeek
0

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 :)

Edited by Geekitygeek: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.