Skip to content

Query: Error calling Count() after GroupJoin() #7497

Closed

Description

@john-luke-laue

I'm getting a runtime error after doing a Count() on a GroupJoin()

Steps to reproduce

var query = _context.Users.GroupJoin(_context.Customers, u => u.Id, c => c.UserId, (u, c) => u);

//Error occurs on this line
int count = query.Count();

Exception message:
Expression of type 'System.Collections.Generic.IEnumerable'1[System.Collections.Generic.IEnumerable'1[MyClass]]' cannot be used for return type 'System.Collections.Generic.IEnumerable`1[System.Int32]'

Stack trace:
at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection'1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable'1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, Boolean tailCall, IEnumerable'1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, ParameterExpression[] parameters) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambdaTResults at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0'1.b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func'1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable'1 source) at MyProject.Services.User.UserService.SearchUsers(ContactSearchCriteriaDto cscd) in C:\Users\john-luke.laue\repositories\MyProject\src\Services\User\UserService.cs:line 59

Further technical details

EF Core version: 1.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2015

Activity

  1. smitpatel commented on Jan 27, 2017

    @smitpatel
    Contributor
    public class Blog
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public List<Post> Posts { get; set; }
    }
    
    public class Post
    {
        public int Id { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
    
    using (var db = new MyContext())
    {
        // Run queries
    
        var query = db.Set<Blog>().GroupJoin(db.Set<Post>(), blog => blog.Id, post => post.BlogId,
            (blog, posts) => blog);
    
        var count = query.Count();
    }
  2. added this to the 2.0.0 milestone on Jan 31, 2017
  3. smitpatel commented on Feb 15, 2017

    @smitpatel
    Contributor

    Transferring to @maumar ,
    In this particular case, we have a group join but there is no selectmany afterwards. We apply count assuming it is safe but it generates query like this:

    SELECT COUNT(*)
    FROM [Blogs] AS [blog]
    LEFT JOIN [Post7497] AS [post] ON [blog].[Id] = [post].[BlogId]
    

    Which is incorrect since it would count number of rows instead of number of groups. We need to evaluate Count on client when there is group join without select many.

  4. added 4 commits that reference this issue on Mar 10, 2017
  5. maumar commented on Mar 23, 2017

    @maumar
    Contributor

    Fixed in 03a990c

  6. changed the title [-]Error calling Count() after GroupJoin()[/-] [+]Query: Error calling Count() after GroupJoin()[/+] on May 9, 2017
  7. modified the milestones: , 2.0.0 on Oct 15, 2022
  8. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions