Skip to content

Commit

Permalink
Disabling ordering when using count() operator in Linq statement gene…
Browse files Browse the repository at this point in the history
…ration. Closes GH-2580
  • Loading branch information
jeremydmiller committed Nov 26, 2023
1 parent 1b6f036 commit 13a3c65
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/LinqTests/Operators/any_operator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public void naked_any_hit()
theSession.Query<Target>().Any().ShouldBeTrue();
}

[Fact]
public async Task any_should_ignore_order()
{
theSession.Store(new Target { Number = 1 });
theSession.Store(new Target { Number = 2 });
theSession.Store(new Target { Number = 3 });
theSession.Store(new Target { Number = 4 });
await theSession.SaveChangesAsync();

(await theSession.Query<Target>().OrderBy(x => x.Number).AnyAsync()).ShouldBeTrue();
}

[Fact]
public void any_hit_with_only_one_document()
{
Expand Down
12 changes: 12 additions & 0 deletions src/LinqTests/Operators/count_operator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public void count_without_any_where()
theSession.Query<Target>().Count().ShouldBe(4);
}

[Fact]
public async Task count_ignores_order_by()
{
theSession.Store(new Target { Number = 1 });
theSession.Store(new Target { Number = 2 });
theSession.Store(new Target { Number = 3 });
theSession.Store(new Target { Number = 4 });
await theSession.SaveChangesAsync();

(await theSession.Query<Target>().OrderBy(x => x.Number).CountAsync()).ShouldBe(4);
}

[Fact]
public void long_count_without_any_where()
{
Expand Down
4 changes: 4 additions & 0 deletions src/Marten/Linq/CollectionUsage.Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ internal void ProcessSingleValueModeIfAny(SelectorStatement statement)
break;

case Marten.Linq.Parsing.SingleValueMode.Count:
// Invalid to be using OrderBy() while also using Count() in
// PostgreSQL. Thank you Hot Chocolate.
statement.Ordering.Expressions.Clear();

if (statement.IsDistinct)
{
throw new NotImplementedException("Not yet");
Expand Down

0 comments on commit 13a3c65

Please sign in to comment.