如果我想使用Dapper獲取包含單個int
列的一堆行,並且此結果集可能為空。使用Dapper查詢此數據的最佳方法是什麼?
例如,如果我有以下方法返回我想要的:
public void int[] GetInts()
{
conn.Query<int?>("select 123 where 1=1")
.Where(x=> x.HasValue)
.Select(x => x.Value)
.ToArray();
}
如果我將行更改為:
conn.Query<int>("select 123 where 1=0").ToArray();
沒有結果時,我收到一個轉換錯誤。
堆棧跟踪在下面,並且異常只是(T)next
轉換(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