Different SQL produced from Where(l => l.Side == 'A') vs Where(l => l.Side.Equals('A')

给你一囗甜甜゛ 提交于 2019-12-03 10:46:27

There is some documentation about this:

Mismatches in SQL Server: Fixed length character types. Transact-SQL distinguishes between Unicode and non-Unicode categories and has three distinct types in each category: fixed length nchar/char, variable length nvarchar/varchar, and larger-sized ntext/text. The fixed length character types could be mapped to the CLR System.Char type for retrieving characters, but they do not really correspond to the same type in conversions and behavior.

And the L2S source code has only one place that uses the string literal "UNICODE":

It appears that a necessary precondition for the function to show up is a SqlUnary syntax tree node with type Convert:

I don't know how you managed to satisfy the IsNumeric condition. I think you have a type mismatch there. Is the column really mapped as System.Char?

The Equals call probably does not trigger this code path. This likely is a L2S bug.

Equals is translated in multiple places in the source code. Here is one of them:

It looks like this bypasses any argument conversions. It does not care what the argument is. This probably fails with a variety of queries (so it's likely a bug). I wonder what happens if you write l.Side.Equals(1.2m). I guess this translates literally to SQL.


I have now reproduced it. Map the column to string. This fixes the generated SQL. The execution plan shows that an index seek is possible with the SQL that is being generated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!