2

When I make a complex query and try to filter out the data on a varchar type column, the provider converting in sql code the required variable is not in the "where" block, and declares a variable of type NVarchar and assigns it a value. And block "where" adds search for that variable.

--  SqlServer.2008
DECLARE @cashRegisterNumber NVarChar -- String
SET     @cashRegisterNumber = N'0705311'

Because default nvarchar type has a length of 1, it looks for the first character. How to overcome and make it look the whole line?

Redliru
  • 21
  • 2
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – lokusking Jul 04 '16 at 05:46

1 Answers1

2

Try Sql.AsSql method:

var str = "0705311";

...Where(t => t.Field1 == Sql.AsSql(str));
IT.
  • 859
  • 4
  • 11
  • It's not work. Converter incorrectly declares a variable of type string. If we add the value directly in the where block, then everything is OK, but it's not the solution. – Redliru Jul 05 '16 at 03:12