Skip to content

Simple VB Group By clause throws #30773

@bricelam

Description

@bricelam
From o In db.Orders
Group By o.CustomerID Into g = Group
Select g.Count()

InvalidOperationException : The LINQ expression '$VB$ItAnonymous' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Expression:

DbSet<Order>()
    .GroupBy(
        keySelector: o => o.CustomerID, 
        resultSelector: (CustomerID, $VB$ItAnonymous) => new VB$AnonymousType_0<string, IEnumerable<Order>>(
            CustomerID = CustomerID, 
            g = $VB$ItAnonymous
        ))
    .Select($VB$It => $VB$It.g
        .Count())

🩹 Workaround

Rewriting it using method calls to avoid the problematic part of the expression tree works.

dbContext.Orders _
    .GroupBy(Function(o) o.CustomerID) _
    .Select(Function(g) g.Count())
SELECT COUNT(*)
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions