Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trouble with casting
Message
 
To
23/09/2014 13:35:40
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01608054
Message ID:
01608105
Views:
26
This message has been marked as a message which has helped to the initial question of the thread.
>>>The last code doesn't compile. It says operator ?? can not be applied to short and short.
>>
>>Ah, too bad. I would have expected that to work, but it looks like the operators are weird for shorts. I never would have expected the large difference in behavior between them and ints.
>
>I think its because shorts aren't nullable. You would probably have the same error with an int.

I assumed Max returned a Nullable<T> But it looks like it doesn't (and will actually fail if your query doesn't return any records).

And shorts are nullable (well, not by default but you can do: short? val = null;). That's kind of why I would have expected it to work, since this does:
short? x = null;
short y = 2;

var result = x ?? y;
That means my other post about ?? working with shorts isn't really correct. It does, but one of them has to be nullable which in the case of Max(), wasn't the case.

Anyway, the original query could be rewritten as this, which would avoid an exception when no records are returned:
var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=> (short?)sp.LocSuffix) ?? 0;
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Reply
Map
View

Click here to load this message in the networking platform