Sending dynamically created SQL Parameters into dapper.
I usually do the following when sending multiple params to dapper:
return connection.Query<Customer>(sql.ToString(),
new
{
Status = status,
ZipCodes = zipCodes,
Types = type
}).ToList();
However I need to figure out a way to dynamically generate these parameters based on a list of key value pairs that I will have.
I want to be able to loop through a list and create the params to pass into dapper dynamically.
Dynamic parameters can be strings, measure values, or dates, offering flexibility and interactivity when building a dashboard for your audience. Because they can be easily used in most analytical entities, dynamic parameters give you programmatic control to customize your analysis.
Returns an enumerable of the type specified by the T parameter. QueryAsync. Returns an enumerable of dynamic types asynchronously. QueryAsync<T> Returns an enumerable of the type specified by the T parameter asynchronously.
Here's how I would modify what you've got. In this snippet, Params is your dictionary.
DynamicParameters dbParams = new DynamicParameters();
dbParams.AddDynamicParams(params);
return connection.Query<Customer>(sql.ToString(),dbParams).ToList();
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