Skip to content

Commit 9fcbb3b

Browse files
committed
Fix/1480 fix ReadFirstOrDefaultAsync throw exception
1 parent 6ab170c commit 9fcbb3b

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

Dapper/SqlMapper.GridReader.Async.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.Common;
5+
using System.Globalization;
56
using System.Linq;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -210,7 +211,20 @@ private async Task<T> ReadRowAsyncImplViaDbReader<T>(DbDataReader reader, Type t
210211
deserializer = new DeserializerState(hash, GetDeserializer(type, reader, 0, -1, false));
211212
cache.Deserializer = deserializer;
212213
}
213-
result = (T)deserializer.Func(reader);
214+
215+
object val = deserializer.Func(reader);
216+
217+
if (val != null)
218+
{
219+
if (val is T)
220+
result = (T)val;
221+
else
222+
{
223+
var convertToType = Nullable.GetUnderlyingType(type) ?? type;
224+
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
225+
}
226+
}
227+
214228
if ((row & Row.Single) != 0 && await reader.ReadAsync(cancel).ConfigureAwait(false)) ThrowMultipleRows(row);
215229
while (await reader.ReadAsync(cancel).ConfigureAwait(false)) { /* ignore subsequent rows */ }
216230
}

Dapper/SqlMapper.GridReader.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,18 @@ private T ReadRow<T>(Type type, Row row)
182182
cache.Deserializer = deserializer;
183183
}
184184
object val = deserializer.Func(reader);
185-
if (val == null || val is T)
186-
{
187-
result = (T)val;
188-
}
189-
else
185+
186+
if (val != null)
190187
{
191-
var convertToType = Nullable.GetUnderlyingType(type) ?? type;
192-
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
188+
if (val is T)
189+
result = (T)val;
190+
else
191+
{
192+
var convertToType = Nullable.GetUnderlyingType(type) ?? type;
193+
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
194+
}
193195
}
196+
194197
if ((row & Row.Single) != 0 && reader.Read()) ThrowMultipleRows(row);
195198
while (reader.Read()) { /* ignore subsequent rows */ }
196199
}
@@ -363,15 +366,19 @@ private IEnumerable<T> ReadDeferred<T>(int index, Func<IDataReader, object> dese
363366
var convertToType = Nullable.GetUnderlyingType(effectiveType) ?? effectiveType;
364367
while (index == gridIndex && reader.Read())
365368
{
369+
T result = default;
366370
object val = deserializer(reader);
367-
if (val == null || val is T)
371+
if (val != null)
368372
{
369-
yield return (T)val;
370-
}
371-
else
372-
{
373-
yield return (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
373+
if (val is T)
374+
result = (T)val;
375+
else
376+
{
377+
result = (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
378+
}
374379
}
380+
381+
yield return result;
375382
}
376383
}
377384
finally // finally so that First etc progresses things even when multiple rows

0 commit comments

Comments
 (0)