Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trouble with casting
Message
De
23/09/2014 13:15:08
 
 
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:
01608083
Vues:
20
This message has been marked as a message which has helped to the initial question of the thread.
J'aime (1)
>>>>>>It doesn't work because the compiler knows that when you add 2 shorts together you could overflow (so it needs an int/Int32 to store the result). It wants you to specifically cast the result of the addition to a (short) before it gets assigned to newSuffix so it knows that you understand the consequences that you could possibly lose data. If you want it as one line:
>>>>>>
>>>>>>
>>>>>>newSuffix = Convert.ToInt16(lastLocSuffix) + 1;
>>>>>>
>>>>>
>>>>>Thanks, Paul. I think I tried that version already and got the same problem. Two lines assignment works.
>>>>
>>>>Doh! Yeah, I messed that up when I tested it. I set newSuffix as an int, not a short. BTW - you never actually said what type lastLocSuffix was.
>>>
>>>In the database the column is smallint and defined as short in the class definition.
>>>
>>>Anyway, this now works fine, thanks again to both of you:
>>>
>>>
>>>var lastLocSuffix = _salespointAdapter.GetAll().Max(sp=>sp.LocSuffix);
>>>            Int16 newSuffix = 1;
>>>            if (lastLocSuffix != null)
>>>            {
>>>                newSuffix = Convert.ToInt16(lastLocSuffix);
>>>                newSuffix++;
>>>            }
>>>
>>>            salespoint.LocSuffix = newSuffix;
>>
>>Depending on what GetAll does, you may be bringing the entire table client side to get that single value. If you have access to the DbSet or an IQueryable, the Max function will do the equivalent Sql Max instead.
>>
>>Also be aware that without any locking on the database, with multiple users you may end up with duplicates.
>
>Thanks, Rob. The GetAll of the adapter calls the same method of the repository and in this particular case it's calling base class method which is just
>
> public virtual IEnumerable<T> GetAll()
>        {
>            return _dbSet;
>        }
>
>
>
>So, I assume this will not bring the whole database, right? (this particular table is small anyway).
>
>I'll look for the locking samples if we have them already. The probability is small, though, as this table is not going to be updated/changed often.

Assuming that _dbSet is just a reference to your DbSet, then it will just do the Sql Max.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform