linq invalidoperationException调用TOLIST()
我想使用下面的代码在数据库中的表中更新记录:
context.Events
.Where(eventDb => !events.Any(@event => eventDb.Guid == @event.Guid))
.ToList()
.ForEach(eventDb => eventDb.IsCurrent = false);
但是每次调用Tolist()我会收到以下错误,这对我来说尚不清楚:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于您要求SQL Server在
Events
上执行.yany()
操作,这大概是某种内存中的C#object,并且不使用存在于SQL Server上,太复杂了,无法发送到SQL。如果将GUID从
Events
提取到一个简单的字符串列表中,则可以简单地将其发送到SQL。您还需要稍微更改LINQ查询,但我认为这将为您提供所需的结果。I think the problem is that you are asking the SQL server to perform a
.Any()
operation onevents
which is presumably some sort of in-memory C# object and does not exist on the SQL server and is too complicated to be sent to SQL.If you extract the Guids from
events
into a simple list of strings, then that could be simple enough to send to sql. You also need to change your linq query slightly, but I think this will give you the results you are looking for.我认为问题是eventdb.guid == @event.guid无法转换为数据库RAW SQL。
GUID是什么类型?
查看如果将Tolist()移至WHERE子句之前会发生什么。
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.