Skip to content

Commit

Permalink
Query: Convert List.Count to Queryable.Count() only for queryables
Browse files Browse the repository at this point in the history
For collection scalar property preserve Count member as is.

Resolves #19715
  • Loading branch information
smitpatel committed Feb 7, 2020
1 parent 24b0c3c commit 3a91c13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
13 changes: 12 additions & 1 deletion test/EFCore.Specification.Tests/CustomConvertersTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CollectionScalar> .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<InvalidOperationException>(
() => context.Set<CollectionScalar>().Where(e => e.Tags.Count == 2).ToList())
.Message.Replace("\r", "").Replace("\n", ""));
}

protected class CollectionScalar
{
public int Id { get; set; }
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 3a91c13

Please sign in to comment.