How to fix the When passing parameters by position, each parameter can only be referenced once Dapper error

I lately had to write some queries using Dapper which use a date, defined from the user, and check this date against two other from-to dates. In other words the query tries to find if a given date is inside the range of two other dates. When I tried to use the same date-parameter to Dapper, I was getting the following exception: When passing parameters by position, each parameter can only be referenced once.

To solve this problem I had to use two different names for the same given date (DateSelected in the following example) and pass these two new parameters into Dapper builder.Where method:

var builder = new SqlBuilder();

// Code for SELECT

builder.Where("DateFrom <= @DateFrom AND DateTo >= @DateTo",
              new { DateFrom = DateSelected, DateTo = DateSelected });
comments powered by Disqus