If I want to get a bunch of rows containing a single column of int
, using Dapper, and it's possible for this result set to be empty. What's the best way to use Dapper to query 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 change 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 an object reference not set to instance of an object 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
x => v.Value
has a typo and.Query<int>
in conjunction withx.HasValue
is not legal either.) And how current is your version of Dapper? – Kirk Woll May 25 '12 at 20:54