I have the following code that uses Dapper:
public async Task GetClientsInvoices(List<long> clients, List<int> invoices)
{
var parameters = new
{
ClientIds = clients,
InvoiceIds = invoices
};
string sqlQuery = "SELECT ClientId, InvoiceId, IsPaid" +
"FROM ClientInvoice " +
"WHERE ClientId IN ( @ClientIds ) " +
"AND InvoiceId IN ( @InvoiceIds )";
var dbResult = await dbConnection.QueryAsync<ClientInvoicesResult>(sqlQuery, parameters);
}
public class ClientInvoicesResult
{
public long ClientId { get; set; }
public int InvoiceId { get; set; }
public bool IsPaid { get; set; }
}
which produces this based on sql server profiler
exec sp_executesql N'SELECT ClientId, InvoiceId, IsPaid FROM ClientInvoice WHERE ClientId IN ( (@ClientIds1) ) AND InvoiceId IN ( (@InvoiceIds1,@InvoiceIds2) )',N'@InvoiceIds1 int,@InvoiceIds2 int,@ClientIds1 bigint',InvoiceIds1=35,InvoiceIds2=34,ClientIds1=4
When it is executed I am getting the following error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
I have two questions:
What am I doing wrong and I am getting this exception?
Why dapper translates this query and executes it using
sp_executesql. How do I force it to use a normal select query likeSELECT ClientId, InvoiceId, IsPaid FROM ClientInvoice WHERE ClientId IN (4) AND InvoiceId IN (34,35)