Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dapper

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?

like image 448
Zelid Avatar asked Aug 12 '14 11:08

Zelid


1 Answers

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]
like image 181
Marc Gravell Avatar answered Sep 16 '22 15:09

Marc Gravell