@@ -118,7 +118,7 @@ public override long GetCount()
118
118
119
119
return _overallCount . Value ;
120
120
}
121
-
121
+
122
122
protected string BuildSortClause ( SortField [ ] sortFields )
123
123
{
124
124
if ( sortFields != null && sortFields . Length > 0 )
@@ -142,13 +142,8 @@ protected virtual IList<T> ExecuteQuery(string sql)
142
142
{
143
143
IEnumerable < T > result = _map ( _getConnection ( ) , _getTransaction != null ? _getTransaction ( ) : null , sql , _param ) ;
144
144
145
- if ( _hasOverallCount )
146
- {
147
- IHasOverallCount overallCount = result as IHasOverallCount ;
148
-
149
- if ( overallCount != null )
150
- _overallCount = overallCount . OverallCount ;
151
- }
145
+ if ( result is IHasOverallCount overallCount )
146
+ _overallCount = overallCount . OverallCount ;
152
147
153
148
IList < T > results = result . ToList ( ) ;
154
149
@@ -167,16 +162,29 @@ protected IEnumerable<T> DefaultMap(IDbConnection cn, IDbTransaction tx, string
167
162
{
168
163
IEnumerable < T > result = cn . Query < T > ( sql , param , tx ) ;
169
164
170
- if ( _hasOverallCount )
165
+ var firstRow = result . FirstOrDefault ( ) ;
166
+ long ? overallCount = null ;
167
+
168
+ if ( firstRow == null )
169
+ {
170
+ overallCount = 0 ;
171
+ }
172
+ else if ( _hasOverallCount && firstRow is IHasOverallCount iHasOverallCount )
173
+ {
174
+ overallCount = iHasOverallCount . OverallCount ;
175
+ }
176
+ else if ( _sql . IndexOf ( "OverallCount" , StringComparison . InvariantCultureIgnoreCase ) != - 1
177
+ && firstRow is IDictionary < string , object > dapperRow )
178
+ {
179
+ overallCount = Convert . ToInt64 ( dapperRow [ "OverallCount" ] ) ;
180
+ }
181
+
182
+ if ( overallCount != null )
171
183
{
172
- IHasOverallCount firstRow = result . FirstOrDefault ( ) as IHasOverallCount ;
173
184
ListPage < T > lp = new ListPage < T > ( ) ;
174
185
175
- if ( firstRow != null )
176
- {
177
- lp . OverallCount = firstRow . OverallCount ;
178
- lp . AddRange ( result ) ;
179
- }
186
+ lp . OverallCount = overallCount . Value ;
187
+ lp . AddRange ( result ) ;
180
188
181
189
result = lp ;
182
190
}
0 commit comments