'LINQ InvalidOperationException when calling ToList()
I want to update records in a table in my database using the code below:
context.Events
.Where(eventDb => !events.Any(@event => eventDb.Guid == @event.Guid))
.ToList()
.ForEach(eventDb => eventDb.IsCurrent = false);
But every time on calling ToList() I'm getting the following error, which isn't clear to me:
System.InvalidOperationException: The LINQ expression 'event => EntityShaperExpression:
WawAPI.Models.Event
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.Guid == event.Guid' 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'.
I'm not sure what I'm doing wrong and I'd appreciate any help.
Solution 1:[1]
I think the issue is that EventDb.Guid == @event.Guid can't be converted to the database raw SQL. What type are the Guid's? See what happens if you move the ToList() to before the Where clause.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stewart |
