Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing dynamically created SQL Parameters into dapper as an anonymous type

Tags:

c#

dapper

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.

like image 632
Blake Rivell Avatar asked Jul 18 '16 18:07

Blake Rivell


People also ask

What is Dynamicparameters?

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.

What is QueryAsync in dapper?

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.


1 Answers

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();
like image 161
Tofystedeth Avatar answered Dec 01 '22 23:12

Tofystedeth