Expression trees seem to build an unnecessary conversion when working with bytes and shorts, they convert both sides (in binary expressions for instance) to int32.
This
To answer your question:
Why Expression trees seem to build an unnecessary conversion when working with bytes and shorts... So the question is, what is the reason for this behavior?
The answer is hidden in the fact, that C# types short
, ushort
, byte
and sbyte
lack the arithmetic, comparison... operators:
Extract: 4.1.5 Integral types
For the binary +, –, *, /, %, &, ^, |, ==, !=, >, <, >=, and <= operators, the operands are converted to type
T
, whereT
is the first ofint
,uint
,long
, andulong
that can fully represent all possible values of both operands. The operation is then performed using the precision of typeT
, and the type of the result isT
(or bool for the relational operators). It is not permitted for one operand to be of type long and the other to be of type ulong with the binary operators.
The 7.9.1 Integer comparison operators describes available operators and their operands
bool operator ==(int x, int y);
bool operator ==(uint x, uint y);
bool operator ==(long x, long y);
bool operator ==(ulong x, ulong y);
... // other operators, only for int, uint, long, ulong
The conversion is done for you by Compiler (the reason why you succeed to build that without explicit conversion)
Because there are no operators working with short... the conversion must be applied. And of course, it later depends on the LINQ provider, how to convert such "expression" into SQL.