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
です。
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