Hmm, love nullable types :) But sometimes they can be a little tricky. When working with Linq, you sometimes create anonymous types as a return. If the field that you bring back is a nullable column in the database, it can mean that the anonymous type created is a nullable type as well. The next computation you use can check to see if the value is null, and if it's not, assign it to a new variable. Not saying this is the best way or even the only way, but it worked for me, and I liked it :)
int value = (myNullableValue.HasValue) ? 0 : myNullableValue.Value;
What does it all mean? It's a ternary operation.
If the first value is true ((myNullableValue.HasValue) ? 0), return that, otherwise return the second expression (myNullableValue.Value)
Otherwise if you've already done a nullable check ...
int value = (int)myNullableValue;
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment
Note: Only a member of this blog may post a comment.