0

This should be really simple. I want a Dictionary with a list of records from a Customers table with that property/field value (RegionCode).

I thought this would work (works fine in LinqPad):

await db.Customers
 .GroupBy(o => o.RegionCode)
 .Select(o => new { o.Key, Customers = o.ToList() })
 .ToDictionaryAsync(o => o.Key, o => o.Customers)

But I get:

System.InvalidOperationException: The LINQ expression 'ToList<Customer>(GroupByShaperExpression:
KeySelector: c.RegionCode, 
ElementSelector:EntityShaperExpression: 
    EntityType: Customer
    ValueBufferExpression: 
        ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False
)' 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.

Basically, similar to this question but in Linq-to-SQL.

Any ideas?

Sean
  • 14,359
  • 13
  • 74
  • 124
  • 1
    Have you tried: `.AsEnumerable().ToDictionaryAsync(o => o.Key, o => o.Customers)` – TanvirArjel Mar 26 '20 at 14:27
  • This is due to a breaking change in EF Core 3.0. You can read more [here](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client) – Magnetron Mar 26 '20 at 14:30

0 Answers0