如果我想使用Dapper獲得一堆包含單個列int
的行,並且此結果集可能爲空。使用Dapper查詢這些數據的最佳方法是什麼?如何使用dapper獲得可空int []?
例如,如果我有以下方法,返回我想要什麼:
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
時不設置到對象的實例是一個對象引用:
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'有一個錯字,'.Query'與'x.HasValue'一起使用也不合法)。你的Dapper版本的當前版本是多少? –
@KirkWoll今天剛剛更新了Dapper,以確保它不是版本問題。 – scottm