Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 46 additions & 0 deletions src/LinqTests/ChildCollections/query_against_child_collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,52 @@ private async Task buildUpTargetData()
await theSession.SaveChangesAsync();
}

[Fact]
public async Task bug_3392_can_with_deeper_boolean_nesting(){
//Test case based on https://github.com/JasperFx/marten/issues/3392
await buildUpTargetData();
bool isTrue = true;
bool isFalse = false;
var random = new Random();
var randomtarget = targets[random.Next(targets.Length)];
var intList = new List<int>(){randomtarget.Number};
//We are using the NestedObject collection here as we know it always has elements.
var somenestedtarget = randomtarget.NestedObject.Targets.First();

Expression<Func<Target, bool>> predicate = x => (isFalse || (isTrue && x.NestedObject.Targets.Any(z => z.AnotherString.Contains(somenestedtarget.AnotherString)))) && isTrue;

var result = await theSession.Query<Target>().Where(predicate).ToListAsync();
result.Count.ShouldBeGreaterThan(0);
result.ShouldAllBe(predicate);
}

[Fact]
public async Task bug_3710_can_query_childcollection_inside_nested_or(){
//Test case based on https://github.com/JasperFx/marten/issues/3710
await buildUpTargetData();
var random = new Random();
var randomtarget = targets[random.Next(targets.Length)];
// We are using the NestedObject collection here as we know it always has elements.
var somenestedtarget = randomtarget.NestedObject.Targets.First();
//This is a query on the form (subquery or something) and subquery
Expression<Func<Target, bool>> predicate = p =>
(
p.NestedObject.Targets.Any(x => x.String.Contains(somenestedtarget.String))
||
p.String == randomtarget.String
)
&&
p.NestedObject.Targets.Any(x => x.String.Contains(somenestedtarget.String));


var result = await theSession.Query<Target>()
.Where(predicate)
.ToListAsync();

result.Count.ShouldBeGreaterThan(0);
result.ShouldAllBe(predicate);
}

[Fact]
public async Task can_query_with_containment_operator()
{
Expand Down
23 changes: 14 additions & 9 deletions src/Marten/Linq/SqlGeneration/SelectorStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,24 @@ public override string ToString()
return $"Selector statement: {SelectClause}";
}

private void processCompoundRecurcively(CompoundWhereFragment compound, IMartenSession session){
// See https://github.com/JasperFx/marten/issues/3025
foreach (var deepCompound in compound.Children.OfType<CompoundWhereFragment>())
{
processCompoundRecurcively(deepCompound, session);
foreach (var subQueryFilter in deepCompound.Children.OfType<ISubQueryFilter>())
{
subQueryFilter.PlaceUnnestAbove(session, this);
}
}
}

protected override void compileAnySubQueries(IMartenSession session)
{
if (Wheres[0] is CompoundWhereFragment compound)
{
processCompoundRecurcively(compound, session);

if (compound.Children.OfType<ISubQueryFilter>().Any())
{
var subQueries = compound.Children.OfType<ISubQueryFilter>().ToArray();
Expand Down Expand Up @@ -148,15 +162,6 @@ protected override void compileAnySubQueries(IMartenSession session)
foreach (var subQuery in subQueries) subQuery.PlaceUnnestAbove(session, this);
}
}

// See https://github.com/JasperFx/marten/issues/3025
foreach (var deepCompound in compound.Children.OfType<CompoundWhereFragment>())
{
foreach (var subQueryFilter in deepCompound.Children.OfType<ISubQueryFilter>())
{
subQueryFilter.PlaceUnnestAbove(session, this);
}
}
}
else if (Wheres[0] is ISubQueryFilter subQuery)
{
Expand Down