Dapper:如果列名是“ count(*)”,如何从DapperRow获取值? [英] Dapper: How to get value from DapperRow if column name is "count(*)"?
问题描述
我从 Dapper 查询获得了动态结果,其中包含类似的记录:
I have a dynamic result from Dapper query that contains records like this:
{DapperRow, billing_currency_code = 'USD', count(*) = '6'}
我可以使用 rowVariable.billing_currency_code
要获得 6值,我尝试了 rowVariable [ count(*)]
和 rowVariable.kv [ count(*)]
不幸的是没有任何作用...
To get '6' value I tried rowVariable["count(*)"]
and rowVariable.kv["count(*)"]
and unfortunately nothing works...
我无法更改 count(*)
列名
如何从中获取'6'值 > DapperRow 在这种情况下?
How to get the '6' value from the rowVariable
of type DapperRow in such case?
推荐答案
如果列名实际上是 count(*)
,则可以将行强制转换为字典:
If the column name genuinely is "count(*)"
, then you can cast the row to a dictionary:
var data = (IDictionary<string,object>)row;
object value = data["count(*)"];
要使其正常运行(至少在SQL Server中),您的查询必须类似于:
For that to work (at least, in SQL Server), your query would need to be something like:
select count(*) as [count(*)]
但是,在大多数情况下,列没有名称,在这种情况下:修正查询; p
However, in most cases the column doesn't have a name, in which case: fix your query ;p
实际上,我可能会说要修正您的查询无论如何;使用以下代码会更容易:
Actually, I'd probably say fix your query anyway; the following would be much easier to work with:
select count(*) as [Count]
这篇关于Dapper:如果列名是“ count(*)”,如何从DapperRow获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!