实体框架groupby投掷invalidoperationException

发布于 2025-02-02 03:34:49 字数 929 浏览 4 评论 0原文

我正在尝试groupby基于连接表的结果。

var services = await db.Services.AsNoTracking()
    .Include("Visit")
    .Where(s => !s.Cancelled && s.AmtToPay > 0 && patientIds.Contains(s.Visit.PatientNo) && s.DTS < day)
    .GroupBy(s => s.Visit.PatientNo)
    .ToListAsync(cancellationToken);

不幸的是,此groupby导致异常:

system.invalidoperationException:客户端投影包含对'Microsoft.entityframeworkcore.metadata.ipropertybase'的常数表达式的引用,该表作为参数传递给方法为'valityBuffertryReadValue'的参数。这可能会导致内存泄漏;考虑将此常数分配给局部变量,然后使用查询中的变量。参见 https://go.microsoft.com/fwlink/?有关更多信息。

这并不能真正解释情况。在这种情况下,如何使用groupby

I'm trying to GroupBy my results based on a joined table.

var services = await db.Services.AsNoTracking()
    .Include("Visit")
    .Where(s => !s.Cancelled && s.AmtToPay > 0 && patientIds.Contains(s.Visit.PatientNo) && s.DTS < day)
    .GroupBy(s => s.Visit.PatientNo)
    .ToListAsync(cancellationToken);

Unfortunately this GroupBy causes an exception:

System.InvalidOperationException : The client projection contains a reference to a constant expression of 'Microsoft.EntityFrameworkCore.Metadata.IPropertyBase' which is being passed as an argument to the method 'ValueBufferTryReadValue'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information.

Which doesn't really explain the situation. How do I use GroupBy in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

稳稳的幸福 2025-02-09 03:34:49

选择应在groupby之后添加EntityFramework Core中,

var services = await db.Services.AsNoTracking()
    .Include("Visit")
    .Where(s => !s.Cancelled && s.AmtToPay > 0 && patientIds.Contains(s.Visit.PatientNo) && s.DTS < day)
    .GroupBy(s => s.Visit.PatientNo)
    .Select(s => s.Key)
    .ToListAsync(cancellationToken);

根据 ef core 3.1折断3.1破坏说明

从3.0开始,EF Core仅允许在顶级中表达
投影(查询中的最后一个select()调用)将在
客户。当查询其他任何部分的表达式无法
转换为SQL或参数,抛出异常。

select should be added after groupby in EntityFramework Core

var services = await db.Services.AsNoTracking()
    .Include("Visit")
    .Where(s => !s.Cancelled && s.AmtToPay > 0 && patientIds.Contains(s.Visit.PatientNo) && s.DTS < day)
    .GroupBy(s => s.Visit.PatientNo)
    .Select(s => s.Key)
    .ToListAsync(cancellationToken);

according to ef core 3.1 breaking changes notes

Starting with 3.0, EF Core only allows expressions in the top-level
projection (the last Select() call in the query) to be evaluated on
the client. When expressions in any other part of the query can't be
converted to either SQL or a parameter, an exception is thrown.

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