>>>>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;
>
The last code doesn't compile. It says operator ?? can not be applied to short and short.
If it's not broken, fix it until it is.
My Blog