Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assorted Linq fixes #2857

Merged
merged 16 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/documents/plv8.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ The `Patch.Remove()` operation removes the given item from a child collection:
<!-- snippet: sample_remove_primitive_element -->
<a id='snippet-sample_remove_primitive_element'></a>
```cs
[Fact]public void remove_primitive_element()
[Fact]
public void remove_primitive_element()
{
var random = new Random();
var target = Target.Random();
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/querying/linq/async-enumerable.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task query_to_async_enumerable()
}
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Operators/async_enumerable.cs#L17-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_to_async_enumerable' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Operators/async_enumerable.cs#L18-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_to_async_enumerable' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

::: warning
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/querying/linq/booleans.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public void query_by_booleans(IDocumentSession session)
session.Query<Target>().Where(x => x.Flag == false).ToArray();
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L154-L170' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_boolean_queries' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L158-L174' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_boolean_queries' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/documents/querying/linq/nulls.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public void query_by_nullable_type_nulls(IDocumentSession session)
session.Query<Target>().Where(x => x.Inner == null);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L172-L183' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_by_nullable_types' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L176-L187' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_query_by_nullable_types' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
13 changes: 8 additions & 5 deletions docs/documents/querying/linq/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void select_a_single_value(IDocumentSession session)
session.Query<Target>().Single(x => x.Number == 5);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L134-L152' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_select_a_single_value' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L138-L156' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_select_a_single_value' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Filtering Documents
Expand Down Expand Up @@ -108,9 +108,12 @@ public void order_by(IDocumentSession session)

// You can use multiple order by's
session.Query<Target>().OrderBy(x => x.Date).ThenBy(x => x.Number);

// If you're brave, you can even use raw SQL literals as of Marten v7!
session.Query<Target>().OrderBySql("substring(d.data -> 'String', 1, 2)");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L84-L97' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_ordering-in-linq' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L85-L101' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_ordering-in-linq' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Ordering with dynamic properties
Expand Down Expand Up @@ -143,7 +146,7 @@ public void order_by_dynamic_props(IDocumentSession session)
session.Query<Target>().OrderBy("Date DESC", "Number");
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L99-L123' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_ordering-in-linq-using-dynamic-props' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L103-L127' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_ordering-in-linq-using-dynamic-props' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Case-insensitive ordering for strings
Expand Down Expand Up @@ -190,7 +193,7 @@ public async Task sample_aggregation_operations(IQuerySession session)
var average = await session.Query<Target>().AverageAsync(x => x.Number);
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L186-L201' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_aggregation_operations' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L190-L205' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_aggregation_operations' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Partitioning Operators
Expand All @@ -209,7 +212,7 @@ public void using_take_and_skip(IDocumentSession session)
session.Query<Target>().Skip(10).Take(10).OrderBy(x => x.Number).ToArray();
}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L125-L132' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_take_and_skip' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L129-L136' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_take_and_skip' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

TODO -- link to the paging support
Expand Down
2 changes: 1 addition & 1 deletion docs/documents/querying/linq/projections.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ var results = query.Query<Target>()
.Take(15)
.ToList();
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Acceptance/select_many.cs#L384-L392' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-select-many' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/LinqTests/Acceptance/select_many.cs#L387-L395' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using-select-many' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

A few notes on the `SelectMany()` usage and limitations:
Expand Down
3 changes: 2 additions & 1 deletion docs/documents/querying/linq/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public void case_insensitive_string_fields(IDocumentSession session)
session.Query<Target>().Where(x => x.String.Contains("soMeThiNg", StringComparison.OrdinalIgnoreCase));

session.Query<Target>().Where(x => x.String.Equals("ThE SaMe ThInG", StringComparison.OrdinalIgnoreCase));

}
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L70-L82' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_searching_within_case_insensitive_string_fields' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/LinqExamples.cs#L70-L83' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_searching_within_case_insensitive_string_fields' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

A shorthand for case-insensitive string matching is provided through `EqualsIgnoreCase` (string extension method in *Baseline*):
Expand Down
21 changes: 21 additions & 0 deletions src/EventSourcingTests/Bugs/Bug_2848_distinct_count_query.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Events.Archiving;
using Marten.Testing.Harness;
using Xunit;

namespace EventSourcingTests.Bugs;

public class Bug_2848_distinct_count_query : BugIntegrationContext
{
[Fact]
public async Task can_make_the_query()
{
await theSession.Events.QueryAllRawEvents()
.Where(x => x.MaybeArchived())
.Select(x => x.StreamKey)
.Distinct()
.CountAsync();
}
}
3 changes: 3 additions & 0 deletions src/LinqTests/Acceptance/select_many.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void distinct_and_count()

using (var query = theStore.QuerySession())
{
query.Logger = new TestOutputMartenLogger(_output);
query
.Query<ProductWithList>()
.SelectMany(x => x.Tags)
Expand All @@ -83,6 +84,7 @@ public void distinct_and_count_long()

using (var query = theStore.QuerySession())
{
query.Logger = new TestOutputMartenLogger(_output);
query
.Query<ProductWithList>()
.SelectMany(x => x.Tags)
Expand All @@ -108,6 +110,7 @@ public void can_do_simple_select_many_against_generic_list()

using (var query = theStore.QuerySession())
{
query.Logger = new TestOutputMartenLogger(_output);
var distinct = query.Query<ProductWithList>().SelectMany(x => x.Tags).Distinct().ToList();

distinct.OrderBy(x => x).ShouldHaveTheSameElementsAs("a", "b", "c", "d", "e", "f");
Expand Down
54 changes: 54 additions & 0 deletions src/LinqTests/Bugs/Bug_2810_distinct_plus_include.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Schema;
using Marten.Testing.Harness;
using Xunit.Abstractions;

namespace LinqTests.Bugs;

public class Bug_2810_distinct_plus_include : BugIntegrationContext
{
private readonly ITestOutputHelper _output;

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

[Fact]
public async Task do_not_blow_up()
{
var exportId = 34;
var includedTableDocuments = new List<IncludedTable>();
var skip = 30;
var take = 10;

theSession.Logger = new TestOutputMartenLogger(_output);

var results = await theSession.Query<MainTable>()
.Include(x => x.IncludedTableId, includedTableDocuments)
.Where(i => i.ExportId == exportId)
.OrderBy(i => i.IncludedTableId)
.Select(i => i.IncludedTableId)
.Distinct()
.Skip(skip)
.Take(take)
.ToListAsync();
}
}

public class MainTable
{
[Identity]
public string Id { get; set; }
public string IncludedTableId { get; set; }
public int ExportId { get; set; }
}

public class IncludedTable
{
public string Id { get; set; }
public string OtherProperty { get; set; }
}
31 changes: 31 additions & 0 deletions src/LinqTests/Bugs/Bug_2850_missed_field_reducing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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_2850_missed_field_reducing : BugIntegrationContext
{
public async Task RunQuery(bool include, int resultCount)
{
var results = await theSession.Query<Target>().Where(x => include || !x.Flag).CountAsync();
results.ShouldBe(resultCount);
}

[Fact]
public async Task pass_bool_into_query()
{
await theStore.Advanced.Clean.DeleteDocumentsByTypeAsync(typeof(Target));

var targets = Target.GenerateRandomData(100).ToArray();
await theStore.BulkInsertAsync(targets);

var count = targets.Count(x => x.Flag);

await RunQuery(true, 100);
await RunQuery(false, 100 - count);
}
}
3 changes: 2 additions & 1 deletion src/LinqTests/Bugs/Bug_717_permutation_of_Linq_queries.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Marten;
using Marten.Linq;
using Marten.Testing.Harness;

Expand Down Expand Up @@ -97,4 +98,4 @@ public enum SomeEnums
public Bug_717_permutation_of_Linq_queries(DefaultStoreFixture fixture) : base(fixture)
{
}
}
}
2 changes: 1 addition & 1 deletion src/LinqTests/Operators/aggregate_functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task get_max_async()
theSession.Store(new Target { Color = Colors.Green, Number = 3 });
theSession.Store(new Target { Color = Colors.Blue, Number = 4 });

theSession.SaveChanges();
await theSession.SaveChangesAsync();
var maxNumber = await theSession.Query<Target>().MaxAsync(t => t.Number);
maxNumber.ShouldBe(42);
}
Expand Down
1 change: 1 addition & 0 deletions src/LinqTests/Operators/async_enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;
Expand Down
Loading
Loading