none
Linq converts tinyint to int for null check RRS feed

  • Question

  • I have a table with a column 'Status', tinyint, null and a linq query like

    var i = from x in DataContext where Status == null || Status == 0
    var t = i.Count()

    SQL profiler shows the following query as the end result

    exec sp_executesql N'SELECT COUNT(*) AS [value]
    FROM [dbo].[Table] AS [t0]
    WHERE ((CONVERT(Int,[t0].[Status])) IS NULL) OR ([t0].[Status] = @p0)',N'@p0 int',@p0=0

    Notice the CONVERT in the first predicate. DBML shows Status as a Byte and SQL type as TinyInt so why is it converting to do a null check? How can I prevent this?


    Get FREE tech books (Development and IT)
    Programmers-Unlimited.com
    Monday, December 6, 2010 4:31 PM

Answers

  • Hello,

    The IS NULL is not a "null check" but it is your filter specified in the where clause in the linq query. And yes, tinyint types corresponding Byte  in CLR (click here for type mapping details). null here stands for 0 which is of type Int32, hence, it converts byte to int to avoid any overflow/data loose.

    Regards,


    Roahn Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by liurong luo Tuesday, December 14, 2010 5:08 AM
    Tuesday, December 14, 2010 5:07 AM

All replies

  • Hello,

    The IS NULL is not a "null check" but it is your filter specified in the where clause in the linq query. And yes, tinyint types corresponding Byte  in CLR (click here for type mapping details). null here stands for 0 which is of type Int32, hence, it converts byte to int to avoid any overflow/data loose.

    Regards,


    Roahn Luo [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by liurong luo Tuesday, December 14, 2010 5:08 AM
    Tuesday, December 14, 2010 5:07 AM
  • Hi Roahn,

     

    So what is the null check in this case? I have tried var i = from x in DataContext where !Status.HasValue || Status == (byte)0

     and still the same problem. Any ideas?

     

    I

    Monday, February 7, 2011 1:05 PM