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

Commit dad8a77

Browse files
committed
Add support for Sql.CountDistinct()
1 parent c64c91c commit dad8a77

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/ServiceStack.OrmLite.Sqlite/SqliteExpression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
6262
quotedColName,
6363
args.Count == 1 ? string.Format(",{0}", args[0]) : "");
6464
break;
65+
case "CountDistinct":
66+
statement = string.Format("COUNT(DISTINCT {0})", quotedColName);
67+
break;
6568
default:
6669
throw new NotSupportedException();
6770
}

src/ServiceStack.OrmLite/Expressions/Sql.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public static T Count<T>(T value)
6363
return value;
6464
}
6565

66+
public static T CountDistinct<T>(T value)
67+
{
68+
return value;
69+
}
70+
6671
public static string Count(string value)
6772
{
6873
return "COUNT({0})".Fmt(value);

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,9 @@ protected virtual object VisitSqlMethodCall(MethodCallExpression m)
18141814
quotedColName,
18151815
args.Count == 1 ? string.Format(",{0}", args[0]) : "");
18161816
break;
1817+
case "CountDistinct":
1818+
statement = string.Format("COUNT(DISTINCT {0})", quotedColName);
1819+
break;
18171820
default:
18181821
throw new NotSupportedException();
18191822
}

tests/ServiceStack.OrmLite.Tests/CustomSqlTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Linq;
23
using NUnit.Framework;
34
using ServiceStack.DataAnnotations;
5+
using ServiceStack.OrmLite.Tests.Expression;
46
using ServiceStack.Text;
57

68
namespace ServiceStack.OrmLite.Tests
@@ -203,5 +205,30 @@ public void Can_select_custom_field_expressions()
203205
Assert.That(row.Area, Is.EqualTo(10 * 5));
204206
}
205207
}
208+
209+
[Test]
210+
public void Can_Count_Distinct()
211+
{
212+
using (var db = OpenDbConnection())
213+
{
214+
db.DropAndCreateTable<LetterFrequency>();
215+
216+
var rows = "A,B,B,C,C,C,D,D,E".Split(',').Map(x => new LetterFrequency { Letter = x });
217+
218+
db.InsertAll(rows);
219+
220+
var count = db.Count(db.From<LetterFrequency>().Select(x => x.Letter));
221+
Assert.That(count, Is.EqualTo(rows.Count));
222+
223+
count = db.Scalar<long>(db.From<LetterFrequency>().Select(x => Sql.Count(x.Letter)));
224+
Assert.That(count, Is.EqualTo(rows.Count));
225+
226+
var distinctCount = db.Scalar<long>(db.From<LetterFrequency>().Select(x => Sql.CountDistinct(x.Letter)));
227+
Assert.That(distinctCount, Is.EqualTo(rows.Map(x => x.Letter).Distinct().Count()));
228+
229+
distinctCount = db.Scalar<long>(db.From<LetterFrequency>().Select("COUNT(DISTINCT Letter)"));
230+
Assert.That(distinctCount, Is.EqualTo(rows.Map(x => x.Letter).Distinct().Count()));
231+
}
232+
}
206233
}
207234
}

0 commit comments

Comments
 (0)