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

Simple VB Group By clause throws #30773

Open
bricelam opened this issue Apr 26, 2023 · 0 comments
Open

Simple VB Group By clause throws #30773

bricelam opened this issue Apr 26, 2023 · 0 comments

Comments

@bricelam
Copy link
Contributor

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants