How to return a list of string from a Dapper query?

c# dapper list

Question

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.

1
2
11/8/2017 12:04:01 AM

Expert Answer

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).

3
11/8/2017 12:13:22 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow