How to cast the GetScalar result nicely

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 16-Apr-2013 16:21:11   

Hi All,

How can I get a GetScalar result typed in the cleanest way?

I now use:


            DateTime? toReturn = null;          
            object result = terminals.GetScalar(TerminalFieldIndex.LastRequest, null, AggregateFunction.Max, filter);
            if(result != DBNull.Value)
                toReturn = (DateTime)result;

The above seems like a lot of code to get the value typed.

You could think of something like a TryGetScalar generic that has a 'out' parameter by which you define the type. Is there something like that already, or am I just being stupid by using the code I am using now and is there a better way?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 17-Apr-2013 01:59:36   

GetScalar can't return but object, and hence you will need to cast it as you have done.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-Apr-2013 10:24:26   

(reason is: getscalar can return null, so it can't return a value type) Using queryspec you can tell the fetch logic to pre-cast it for you.

Frans Bouma | Lead developer LLBLGen Pro