If I want to get a bunch of rows containing one column intusing Dapper, and this result set may be empty. What is the best way to use Dapper to request this data?
For example, if I have the following method that returns what I want:
public void int[] GetInts()
{
conn.Query<int?>("select 123 where 1=1")
.Where(x=> x.HasValue)
.Select(x => x.Value)
.ToArray();
}
If I changed the line to this:
conn.Query<int>("select 123 where 1=0").ToArray();
I get a casting error when there are no results.
The stack trace is below, and the exception is just a reference to an object that was not installed in the object instance when casting (T)next:
at Dapper.SqlMapper.<QueryInternal>d__13`1.MoveNext() in .\SqlMapper.cs:line 611
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in .\SqlMapper.cs:line 539
source
share