C# 如何为 Dapper 查询动态创建参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9481678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create arguments for a Dapper query dynamically
提问by Cogslave
I have a dictionary of values Eg "Name": "Alex"
我有一个值字典 例如“名称”:“亚历克斯”
Is there a way to pass this to Dapper as arguments for a query?
有没有办法将它作为查询的参数传递给 Dapper?
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);
采纳答案by Marc Gravell
Yes:
是的:
var dbArgs = new DynamicParameters();
foreach(var pair in args) dbArgs.Add(pair.Key, pair.Value);
Then pass dbArgsin place of args:
然后通过dbArgs代替args:
var stuff = connection.Query<ExtractionRecord>(query, dbArgs);
Alternatively, you can write your own class that implements IDynamicParameters.
或者,您可以编写自己的类来实现IDynamicParameters.
Note that if you are starting from an object (the usual approach with dapper), you can also use this template with DynamicParametersas a starting point:
请注意,如果您从一个对象开始(使用 dapper 的常用方法),您还可以使用此模板DynamicParameters作为起点:
var dbArgs = new DynamicParameters(templateObject);
回答by Casey Crookston
I know this is an old question (like, 5 years old) but I was struggling with the same thing. The complete answer is in the comments to the other answer, but I thought I would offer a full example here.
我知道这是一个老问题(比如 5 岁),但我也在为同样的事情苦苦挣扎。完整的答案在另一个答案的评论中,但我想我会在这里提供一个完整的例子。
string query = "SELECT * FROM MyTableName WHERE Foo = @Foo AND Bar = @Bar";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("@Foo", "foo");
dictionary.Add("@Bar", "bar");
var results = connection.Query<MyTableName>(query, new DynamicParameters(dictionary));
Or, to be fully dynamic, you can create a method like this, which will take any model, any query, and any set of query parameters:
或者,为了完全动态,您可以创建一个这样的方法,它将采用任何模型、任何查询和任何查询参数集:
public static IEnumerable<T> Get<T>(string query, Dictionary<string, object> dictionary)
{
IEnumerable<T> entities = connection.Query<T>(query, new DynamicParameters(dictionary));
return entities;
}
And then to call this method:
然后调用这个方法:
var results = Get<MyTable>(query, dictionary)
EDIT LONG AFTER
很久以后编辑
This answer continues to get upvotes, so this is apparently still a need. I took this solution and created an entire data access NuGet package built on top of Dapper. It reduces your CRUD and query operations to a single line of code.
这个答案继续获得赞成票,所以这显然仍然是必要的。我采用了这个解决方案,并在 Dapper 之上创建了一个完整的数据访问 NuGet 包。它将您的 CRUD 和查询操作减少到一行代码。
Here's the NuGet package.
这是NuGet 包。
回答by turdus-merula
One can also use an ExpandoObjectas the parameters of a query, instead of the Dapper-specific class DynamicParameters:
还可以使用 anExpandoObject作为查询的参数,而不是 Dapper 特定的类DynamicParameters:
ExpandoObject param = new ExpandoObject();
IDictionary<string, object> paramAsDict = param as IDictionary<string, object>;
paramAsDict.Add("foo", 42);
paramAsDict.Add("bar", "test");
MyRecord stuff = connection.Query<MyRecord>(query, param);

