Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generated query for tinyint column introduces a CAST to int

I am querying a tinyint column and entity-framework generates a SELECT query that introduces a CAST to INT for this column even when the value that I am using in the WHERE clause is of the type byte.

Looking the Model, the generated Type for my tinyint column is byte.

Looking the code:

byte byteValue = 6;
var entityList = from r in rep.DataContext.FooTable
                 where r.TinyintColumn == byteValue
                 select r;

Looking the generated query:

SELECT [Extent1].[TinyintColumn] AS [TinyintColumn] WHERE @p__linq__0 = CAST( [Extent1].[TinyintColumn] AS int) 

I have strict restrictions in the performance so I don't want those CAST in any select.

So my question whould be, is there any way to avoid this CAST over the column tinyint? or am I doing something wrong?

Thanks in advance.

like image 601
Belén Morenate Avatar asked Jan 26 '12 09:01

Belén Morenate


People also ask

What is Tinyint data type in mysql?

TINYINT is a very small integer. The minimum and maximum SIGNED values are -128 and 127 respectively, while for UNSIGNED values TINYINT range is from 0 to 255. TINYINT uses 1 byte per row.

When should I use Tinyint in mysql?

If you include 2 or 3, you have to use tinyint (at the very smallest scale). "For example, it's better to use tinyint when you know that the only data you will store is a 1, 0 or null (with a very small chance of expanding that to a 2 or 3 later)." I'd use an ENUM for such a thing.

What is Mediumint?

A medium-sized integer. The signed range is -8388608 to 8388607. The unsigned range is 0 to 16777215. ZEROFILL pads the integer with zeroes and assumes UNSIGNED (even if UNSIGNED is not specified). INT3 is a synonym for MEDIUMINT .


1 Answers

If you use IList<T>.Contains with a List<byte> the Entity Framework won't cast.

List<byte> byteValue = new List<byte> { 6 };
var entityList = from r in rep.DataContext.FooTable
             where byteValue.Contains(r.TinyintColumn)
             select r;

I ran into the same problem and blogged about it.

like image 134
Michaël Hompus Avatar answered Sep 20 '22 11:09

Michaël Hompus