Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trouble with casting
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01608054
Message ID:
01608084
Vues:
25
This message has been marked as a message which has helped to the initial question of the thread.
>>>In the database the column is smallint and defined as short in the class definition.
>>
>>So why is the Convert.ToInt16 needed?
>
>I think it's not needed, I'll remove it. In my original code it was needed as the result was anonymous type.
>
>So, the code now is reduced to
>
>
>var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix);
>            Int16 newSuffix = 1;
>            if (lastLocSuffix != null)
>            {
>                newSuffix = lastLocSuffix++;                
>            }
>
>            salespoint.LocSuffix = newSuffix;
The ++ operator after the variable is done after the assignment (that is, the assignment happens first, then lastLocSuffix is incremented). So if GetAll().Max() returned 1, newSuffix would be assigned a 1 (not a 2).

You would want:
newSuffix = ++lastLocSuffix;
I'd actually end up trying to write the above as:
var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix) ?? (short)0;
salespoint.LocSuffix = ++lastLocSuffix;
actually, this is probably easier to understand and avoids the assignment to lastLocSuffix which probably isn't needed anyway:
var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix) ?? (short)0;
salespoint.LocSuffix = lastLocSuffix + 1;
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform