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

Fix query filters with context accessors #35237

Merged
merged 5 commits into from
Nov 30, 2024
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
6 changes: 5 additions & 1 deletion src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,11 @@ protected override Expression VisitConditional(ConditionalExpression conditional
goto case StateType.ContainsEvaluatable;

case StateType.ContainsEvaluatable:
// The case where the test is evaluatable has been handled above
if (testState.IsEvaluatable)
{
test = ProcessEvaluatableRoot(test, ref testState);
}

if (ifTrueState.IsEvaluatable)
{
ifTrue = ProcessEvaluatableRoot(ifTrue, ref ifTrueState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,4 +730,42 @@ public class ChildFilter2
}

#endregion

#region 35111

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Query_filter_with_context_accessor_with_constant(bool async)
{
var contextFactory = await InitializeAsync<Context35111>();
using var context = contextFactory.CreateContext();

var data = async
? await context.Set<FooBar35111>().ToListAsync()
: context.Set<FooBar35111>().ToList();
}

protected class Context35111(DbContextOptions options) : DbContext(options)
{
public int Foo { get; set; }
public long? Bar { get; set; }
public List<long> Baz { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<FooBar35111>()
.HasQueryFilter(e =>
Foo == 1
? Baz.Contains(e.Bar)
: e.Bar == Bar);
}
}

public class FooBar35111
{
public long Id { get; set; }
public long Bar { get; set; }
}

#endregion
}