Dapper: How to get value from DapperRow if column name is “count(*)”?

泄露秘密 提交于 2019-12-21 03:53:22

问题


I have a dynamic result from Dapper query that contains records like this:

{DapperRow, billing_currency_code = 'USD', count(*) = '6'}

I'm able to access 'USD' by using rowVariable.billing_currency_code

To get '6' value I tried rowVariable["count(*)"] and rowVariable.kv["count(*)"] and unfortunately nothing works...

I can't change the count(*) column name in my case

How to get the '6' value from the rowVariable of type DapperRow in such case?


回答1:


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(*)"];

For that to work (at least, in SQL Server), your query would need to be something like:

select count(*) as [count(*)]

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]



回答2:


Suppose Your Data as below

var Details={DapperRow, billing_currency_code = 'USD', count(*) = '6'}

as The columns is coming dynamically

var firstRow= Details.FirstOrDefault();

To get the heading columns of the data

var Heading= ((IDictionary<string, object>)firstRow).Keys.ToArray();

To get the value of the data by using key

var details = ((IDictionary<string, object>)firstRow);
var vallues= details[Heading[0]];


来源:https://stackoverflow.com/questions/25263701/dapper-how-to-get-value-from-dapperrow-if-column-name-is-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!