I’m doing work with LINQ in C#. Part of the code requires a Sum down a column.
SomeTable.Where( CONDITION ).Sum( Entity => Entity.IntColumn );
This returns a type of int? rather than an int as the table’s column’s type is also of the int? form.
My questions are thus:
What is the int? or double? e&. type?
– What is the best practice for converting or casting those types into their useable int or double e&. counter parts?
int?, double?, etc are nullable types. Nullable types can represent all the values of an underlying type, and an additional null value.
Each instance of a nullable type has two public read-only properties:
HasValueHasValue is of type bool. It is set to true when the variable contains a non-null value.
ValueValue is of the same type as the underlying type. If HasValue is true, Value contains a meaningful value. If HasValue is false, accessing Value will throw a InvalidOperationException.
http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx#Y431