What if you don't need a parameter when querying with Dapper?

dapper

Question

I have one query that does a count/group by where I don't need a parameter (there is no where clause).

What is the syntax to run a parameterless query with dapper?

var _results = _conn.Query<strongType>("Select Count(columnA) as aCount, ColumnB, ColumnC from mytable group by ColumnB, ColumnC");

does not work.

I've tried it a few different ways but I still keep getting "ArgumentNullException was unhandled by user code".

Tried to figure it out myself, searched all over and I'm giving up. Thanks in advance.

Edit: Below is the line of code from SqlMapper.cs that throws the error. It's line 1334

il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));

The error details: Value cannot be null. Parameter name: con

Popular Answer

Mapping a single result back works just fine:

var a = cnn.Query<int>("select 1").Single()
// a is 1

You may face trouble if somehow your query returns no results, for example:

select count(Id) from 
(
select top 0 1 as Id, 2 as Title
) as X
group by Title

return 0 results, so doing a Single on an empty result set is not going to work.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why