Ed R.
Ed R.

Reputation: 181

Simple list of string from Dapper Query

Is there any way to get a simple list of strings from a Dapper Query? I don't want to create an object that contains all my field names by type. My query returns one row of data. Sometimes with 2 columns other times with 5 or 20 or 100 and I just want all the values returned as a single list of strings.

Upvotes: 5

Views: 3768

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

Dapper would make it easy to work with multiple rows, single column, via db.Query<string>(...).

For multiple columns single row, you could try:

var x = db.QuerySingle<(string,string)>(...)

(for two columns; add more items to the tuple for more)

This uses the value-tuple approach to read the data column-wise.

However, this is only good for a handful of columns. If you have hundreds of columns and a single row then I suggest transposing your query (perhaps via PIVOT).

Upvotes: 3

Related Questions