diff --git a/src/LinqTests/ChildCollections/query_against_child_collections.cs b/src/LinqTests/ChildCollections/query_against_child_collections.cs index e137967bd1..c2c1446657 100644 --- a/src/LinqTests/ChildCollections/query_against_child_collections.cs +++ b/src/LinqTests/ChildCollections/query_against_child_collections.cs @@ -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(){randomtarget.Number}; + //We are using the NestedObject collection here as we know it always has elements. + var somenestedtarget = randomtarget.NestedObject.Targets.First(); + + Expression> predicate = x => (isFalse || (isTrue && x.NestedObject.Targets.Any(z => z.AnotherString.Contains(somenestedtarget.AnotherString)))) && isTrue; + + var result = await theSession.Query().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> 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() + .Where(predicate) + .ToListAsync(); + + result.Count.ShouldBeGreaterThan(0); + result.ShouldAllBe(predicate); + } + [Fact] public async Task can_query_with_containment_operator() { diff --git a/src/Marten/Linq/SqlGeneration/SelectorStatement.cs b/src/Marten/Linq/SqlGeneration/SelectorStatement.cs index 734713270e..dce208fc0d 100644 --- a/src/Marten/Linq/SqlGeneration/SelectorStatement.cs +++ b/src/Marten/Linq/SqlGeneration/SelectorStatement.cs @@ -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()) + { + processCompoundRecurcively(deepCompound, session); + foreach (var subQueryFilter in deepCompound.Children.OfType()) + { + subQueryFilter.PlaceUnnestAbove(session, this); + } + } + } + protected override void compileAnySubQueries(IMartenSession session) { if (Wheres[0] is CompoundWhereFragment compound) { + processCompoundRecurcively(compound, session); + if (compound.Children.OfType().Any()) { var subQueries = compound.Children.OfType().ToArray(); @@ -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()) - { - foreach (var subQueryFilter in deepCompound.Children.OfType()) - { - subQueryFilter.PlaceUnnestAbove(session, this); - } - } } else if (Wheres[0] is ISubQueryFilter subQuery) {