Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a dictionary of values Eg "Name": "Alex"

Is there a way to pass this to Dapper as arguments for a query?

Here is an example showing what I want to do.

IDictionary<string, string> args = GetArgsFromSomewhere();
string query = "select * from people where Name = @Name";
var stuff = connection.Query<ExtractionRecord>(query, args);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
282 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes:

var dbArgs = new DynamicParameters();
foreach(var pair in args) dbArgs.Add(pair.Key, pair.Value);

Then pass dbArgs in place of args:

var stuff = connection.Query<ExtractionRecord>(query, dbArgs);

Alternatively, you can write your own class that implements IDynamicParameters.

Note that if you are starting from an object (the usual approach with dapper), you can also use this template with DynamicParameters as a starting point:

var dbArgs = new DynamicParameters(templateObject);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...