Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper returning Nulls for KeyValuePair

Tags:

c#

dapper

We are trying to use Dapper to get a list of records from a SQL database with the following snippet of code:

var result = await conn.QueryAsync<KeyValuePair<string,string>>("Select Tag, Description from Tags");

The result is all the rows from the table, however, both the Tag and the Description are all null.

like image 659
user2981411 Avatar asked Jan 22 '26 16:01

user2981411


1 Answers

Dapper needs to know that the Tag and Description columns should map to the Key and Value properties.
The simplest way to do this is to specify the column aliases directly in the SQL query:

var result = await conn.QueryAsync<KeyValuePair<string, string>>(
    "Select Tag [Key], Description [Value] from Tags");
like image 140
Alexander Petrov Avatar answered Jan 25 '26 13:01

Alexander Petrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!