在 select 查询中使用 new[] 会导致 .NET 8 异常

Using new[] in select query gives exception for .NET 8

提问人:Ogglas 提问时间:11/17/2023 更新时间:11/17/2023 访问量:46

问:

我找不到任何关于此的信息,因此决定发布。

此代码适用于 和 :.NET 7Microsoft.EntityFrameworkCore.SqlServer Version 7.0.13

Updated = p.ThreatAndCountermeasures.Count() > 0 ? new[] { p.Updated, p.ThreatAndCountermeasures.Max(tac => tac.Updated) }.Max() : p.Updated,

With 和 I 出现以下异常:.NET 8Microsoft.EntityFrameworkCore.SqlServer Version 8.0.0

提供程序代码未处理类型为“InlineQueryRootExpression”的查询根。在不支持的其他提供程序上使用特定于提供程序的方法时,会出现此问题。

C# 实体 框架核心 net-8.0

评论


答:

0赞 Ogglas 11/17/2023 #1

new List<>而不是,它会起作用:new[]

Updated = p.ThreatAndCountermeasures.Count() > 0 ? new List<DateTime> { p.Updated, p.ThreatAndCountermeasures.Max(tac => tac.Updated) }.Max() : p.Updated,

GitHub 上的问题:https://github.com/dotnet/efcore/issues/32331

评论

0赞 Panagiotis Kanavos 11/17/2023
目前尚不清楚是否存在 EF 问题或代码问题。这个查询肯定是不寻常的。EF 将 LINQ 查询转换为 SQL,那么这应该生成什么 SQL?在 SQL 中,您也不会在计算 MAX 之前检查。在 SQL Server 2022 中,可以使用类似 .在早期版本中,您必须使用COUNTGREATEST(p.Updated,MAX(other.Updated))IIF(MAX(other.Updated) is NULL OR p.Updated>MAX(other.Updated),p.Updated,MAX(other.Updated))