Skip to content

Commit

Permalink
fixed SelectMany().LongCount() querying. Closes GH-3017
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Mar 7, 2024
1 parent b608a14 commit e8a2248
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/LinqTests/Bugs/Bug_3017_SelectMany_with_LongCount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;

namespace LinqTests.Bugs;

public class Bug_3017_SelectMany_with_LongCount : BugIntegrationContext
{
[Fact]
public async Task can_use_select_many_with_long_count()
{
var targets = Target.GenerateRandomData(1000).ToArray();

await theStore.BulkInsertDocumentsAsync(targets);

var expected = targets.SelectMany(x => x.Children).LongCount();

var actual = await theSession.Query<Target>().SelectMany(x => x.Children)
.LongCountAsync();

actual.ShouldBe(expected);
}

[Fact]
public async Task can_use_select_many_with_integer_count()
{
var targets = Target.GenerateRandomData(1000).ToArray();

await theStore.BulkInsertDocumentsAsync(targets);

var expected = targets.SelectMany(x => x.Children).Count();

var actual = await theSession.Query<Target>().SelectMany(x => x.Children)
.CountAsync();

actual.ShouldBe(expected);
}
}
7 changes: 7 additions & 0 deletions src/Marten/Linq/CollectionUsage.Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ private Statement compileNext(IMartenSession session, IQueryableMemberCollection

selection.ApplyAggregateOperator("SUM");
}
else if (SingleValueMode == Marten.Linq.Parsing.SingleValueMode.LongCount)
{
selection.SelectClause = new NewScalarSelectClause<long>(
$"jsonb_array_length({collectionMember.JSONBLocator})", selection.SelectClause.FromObject);

selection.ApplyAggregateOperator("SUM");
}
else
{
var next = new CollectionUsage(_options, collectionMember.MemberType);
Expand Down

0 comments on commit e8a2248

Please sign in to comment.