Skip to content

Commit

Permalink
Fix for Select() + paging in LINQ. Closes GH-3337
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jul 30, 2024
1 parent ab8ac50 commit 537d0ba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/LinqTests/Bugs/Bug_3337_select_page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Pagination;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;
using Xunit.Abstractions;

namespace LinqTests.Bugs;

public class Bug_3337_select_page : BugIntegrationContext
{
private readonly ITestOutputHelper _output;

public Bug_3337_select_page(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public async Task try_it_out()
{
await theStore.BulkInsertAsync(Target.GenerateRandomData(1000).ToArray());

theSession.Logger = new TestOutputMartenLogger(_output);
var results = await theSession.Query<Target>().Where(x => x.Inner != null)
.Select(x => new SelectedGuy() { Id = x.Id, Number = x.Inner.Number, Text = x.String })
.Take(5).Stats(out var statistics).ToListAsync();
//.ToPagedListAsync(1, 5);

foreach (var result in results)
{
result.Number.ShouldNotBe(0);
result.Id.ShouldNotBe(Guid.Empty);
result.Text.ShouldNotBeNull();
}
}
}

public class SelectedGuy
{
public int Number { get; set; }
public Guid Id { get; set; }
public string Text { get; set; }
}
8 changes: 7 additions & 1 deletion src/Marten/Linq/SqlGeneration/SelectDataSelectClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public void Apply(ICommandBuilder sql)

public string[] SelectFields()
{
return new[] { "data" };
// Fix for GH-3337
var builder = new CommandBuilder();
Selector.Apply(builder);

var fieldName = builder.Compile().CommandText;

return new[] { fieldName };
}

public ISelector BuildSelector(IMartenSession session)
Expand Down

0 comments on commit 537d0ba

Please sign in to comment.