linq invalidoperationException调用TOLIST()

发布于 2025-01-30 20:07:42 字数 784 浏览 3 评论 0原文

我想使用下面的代码在数据库中的表中更新记录:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2025-02-06 20:07:42

我认为问题在于您要求SQL Server在Events上执行.yany()操作,这大概是某种内存中的C#object,并且不使用存在于SQL Server上,太复杂了,无法发送到SQL。

如果将GUID从Events提取到一个简单的字符串列表中,则可以简单地将其发送到SQL。您还需要稍微更改LINQ查询,但我认为这将为您提供所需的结果。

var eventGuids = events.select(a=>a.Guid).ToList();

context.Events
        .Where(eventDb => !eventGuids.Contains(event.Guid))
        .ToList()
        .ForEach(eventDb => eventDb.IsCurrent = false);

I think the problem is that you are asking the SQL server to perform a .Any() operation on events 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.

var eventGuids = events.select(a=>a.Guid).ToList();

context.Events
        .Where(eventDb => !eventGuids.Contains(event.Guid))
        .ToList()
        .ForEach(eventDb => eventDb.IsCurrent = false);
浅黛梨妆こ 2025-02-06 20:07:42

我认为问题是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文