diff --git a/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs b/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs index 0d2a0a43dc2..be9af569afb 100644 --- a/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs +++ b/src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs @@ -158,10 +158,13 @@ protected override Expression VisitMember(MemberExpression memberExpression) { var innerQueryable = UnwrapCollectionMaterialization(innerExpression); - return Visit( + if (innerQueryable.Type.TryGetElementType(typeof(IQueryable<>)) != null) + { + return Visit( Expression.Call( QueryableMethods.CountWithoutPredicate.MakeGenericMethod(innerQueryable.Type.TryGetSequenceType()), innerQueryable)); + } } var updatedExpression = (Expression)memberExpression.Update(innerExpression); diff --git a/test/EFCore.InMemory.FunctionalTests/CustomConvertersInMemoryTest.cs b/test/EFCore.InMemory.FunctionalTests/CustomConvertersInMemoryTest.cs index 9084470650d..e3768b48814 100644 --- a/test/EFCore.InMemory.FunctionalTests/CustomConvertersInMemoryTest.cs +++ b/test/EFCore.InMemory.FunctionalTests/CustomConvertersInMemoryTest.cs @@ -30,6 +30,12 @@ public override void Collection_property_as_scalar_Any() base.Collection_property_as_scalar_Any(); } + [ConditionalFact(Skip = "Issue#17050")] + public override void Collection_property_as_scalar_Count_member() + { + base.Collection_property_as_scalar_Count_member(); + } + [ConditionalFact(Skip = "Issue#17050")] public override void Collection_enum_as_string_Contains() { diff --git a/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs b/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs index defb416aeab..2062e1fd1cd 100644 --- a/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs +++ b/test/EFCore.Specification.Tests/CustomConvertersTestBase.cs @@ -576,6 +576,17 @@ public virtual void Collection_property_as_scalar_Any() .Message.Replace("\r", "").Replace("\n", "")); } + [ConditionalFact] + public virtual void Collection_property_as_scalar_Count_member() + { + using var context = CreateContext(); + Assert.Equal( + @"The LINQ expression 'DbSet .Where(c => c.Tags.Count == 2)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.", + Assert.Throws( + () => context.Set().Where(e => e.Tags.Count == 2).ToList()) + .Message.Replace("\r", "").Replace("\n", "")); + } + protected class CollectionScalar { public int Id { get; set; } @@ -1000,7 +1011,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con b.IndexedProperty(typeof(bool), "IndexerVisible").HasConversion(new BoolToStringConverter("Nay", "Aye")); b.HasData( - new + new { BlogId = 1, Url = "http://blog.com",