Using Dapper, the following throws Incorrect syntax near ','
.
const string sql =
"select * from ZipToZipDistance z where z.NoRouteFound = 0" +
" and z.OriginZip in (@zips) or z.DestZip in (@zips)";
var zipStrings = zips.Select(x => x.ToString()).ToArray();
var result = connection.Query<ZipToZipDistance>(sql,
new { zips = zipStrings });
Hmm, the SQL has no commas. It must have something to do with the parameter. OriginZip
and DestZip
are varchar(10)
. zips
is IEnumerable<int>
. I tried using zips
as the parameter without the converting to strings. Same error.
Seems very straightforward. What am I doing wrong?
Fastest Dapper Plus Extensions The dynamic parameters allow you to specify parameters when you need to send parameters into a stored procedure or an SQL Statment. You can also use the anonymous type method that we have used in the previous articles.
Dynamic parameters allow you to create actions that are different every time they are performed. Dynamic parameters can be passed as arguments to most vizact actions. The value of these parameters is determined when the action is performed, and you can setup these parameters to change values every time.
Dapper is a micro ORM or it is a simple object mapper framework which helps to map the native query output to a domain class or a C# class. It is a high performance data access system built by StackOverflow team and released as open source.
try:
const string sql =
const string sql =
"select * from ZipToZipDistance z where z.NoRouteFound = 0" +
" and z.OriginZip in @zips or z.DestZip in @zips";
var zipStrings = zips.Select(x => x.ToString());
var result = connection.Query<ZipToZipDistance>(sql,
new { zips = zipStrings });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With