带参数列表的Dapper查询 [英] Dapper query with list of parameters
问题描述
我正在尝试使用具有已知参数集的Dapper运行查询,但要使用这些参数的值列表.我正在尝试做的一个简单示例是:
I am trying to run a query with Dapper with a known set of parameters, but with a list of values for those parameters. A simple example of what I am trying to do would be:
DateTime endDate = DateTime.Now;
DateTime startDate = endDate.AddHours(-24);
string query = "select COUNT(*) from Test where Status = @Status AND DateCreated <= @Hour;";
var stuff = con.Query(query, (startDate).ByHourTo(endDate).Select(hour => new
{
Status = 1,
Hour = hour,
}));
Dapper抛出异常,其中必须定义参数'@Status'".我知道Dapper在进行批量插入和更新时可以处理参数列表,但是不能对选择执行此操作吗?
Dapper throws an exception with 'Parameter '@Status' must be defined'. I know Dapper can process lists of parameters when doing bulk inserts and updates, but can it not do this for selects?
推荐答案
啊,我想我明白你的意思了.
Ah, I think I see what you mean...
是的,有一种我们不支持Query的Execute
场景,特别是:使用一系列不同的参数值依次运行相同的操作.这对于Execute
很有意义,但是对于查询来说,这可能意味着您应该使用in
查看其他查询.或者,只需循环并连接.
Yes, there is a scenario we support for Execute
that isn't supported for Query, specifically: to run the same operation sequentially with a range of different parameter values. This makes sense for Execute
, but for query it probably means you should be looking at a different query using in
. Alternatively, just loop and concat.
相反,它正在查看单个参数对象并在寻找公共值-可枚举对象没有适用于dapper的任何合适的参数值.
Instead, it is looking at the single parameter object and looking for public values - an enumerable doesn't have any suitable parameter values for dapper.
这篇关于带参数列表的Dapper查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!