Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 1c21dfb

Browse files
committed
Use subselect to count rows
1 parent bc644ce commit 1c21dfb

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

src/ServiceStack.OrmLite/Expressions/ReadExtensions.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,13 @@ internal static long GetCount(this IDbCommand dbCmd, string sql)
123123

124124
internal static long RowCount<T>(this IDbCommand dbCmd, SqlExpression<T> expression)
125125
{
126-
var sql = expression.Clone().Select("1").ToSelectStatement();
127-
return RowCount(dbCmd, sql);
126+
var sql = "SELECT COUNT(*) FROM ({0}) AS COUNT".Fmt(expression.ToSelectStatement());
127+
return dbCmd.Scalar<long>(sql);
128128
}
129129

130130
internal static long RowCount(this IDbCommand dbCmd, string sql)
131131
{
132-
var rowCount = 0;
133-
using (var reader = dbCmd.ExecReader(sql))
134-
{
135-
while (reader.Read())
136-
{
137-
rowCount++;
138-
}
139-
}
140-
return rowCount;
132+
return dbCmd.Scalar<long>("SELECT COUNT(*) FROM ({0}) AS COUNT".Fmt(sql));
141133
}
142134

143135
internal static List<T> LoadSelect<T>(this IDbCommand dbCmd, Func<SqlExpression<T>, SqlExpression<T>> expression)

0 commit comments

Comments
 (0)