c# - 迁移到 .NET8 后,F# 查询表达式和带有 Array.Contains 的 C# LINQ 失败

标签 c# sql f# .net-8.0 query-expressions

最近我已将代码迁移到 .NET8,但我的一些查询停止工作。 这是一个示例,该示例曾经在 .NET7 中工作

let accounts: Array[] = ...

query {
    for user in db.User do
      where accounts.Contains user.Account
      select user
  }

我收到错误:

Unhandled exception. System.InvalidOperationException: The LINQ expression 
'[Microsoft.EntityFrameworkCore.Query.InlineQueryRootExpression]' could not be translated. 
Additional information: Empty collections are not supported as inline query roots. 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'.

我注意到现在表达式不喜欢空数组,并且在 accounts 时失败。不包含任何元素。所以我需要在各处添加一个守卫,例如

let accounts: Array[] = ...

// This works just fine
query {
    for user in db.User do
      where (accounts.Length <> 0 && accounts.Contains user.Account)
      select user
  }

唯一的问题是在我的代码库中我使用 .Contains方法很多很多次。我真的需要添加部分 accounts.Length <> 0 &&到处或者也许我可以做更好的事情?

我使用 SQL Server 2022。

最佳答案

有一个与此相关的未决问题:Did Contains stop working on empty list in EF Core 8? #32375minimal repo通过 roji

Minimal repro is any composing on top of an empty inline array:

_ = ctx.Blogs.Where(b => new int[] { }.Contains(b.Id)).ToList();

它已在 EF Core 主分支中修复,请参阅 Allow empty inline collections (#32414) ,但仍然存在于 8.0.0 branch of RelationalQueryableMethodTranslatingExpressionVisitor.cs 中。 2023 年 11 月 30 日,修复为 approved包含在服务版本中EF Core 8.0.2 .

因此,您可以等待 EF Core 8.0.2 服务版本,或者,如果您不能等待,请使用 accounts.Length <> 0 && ...解决方法。

关于c# - 迁移到 .NET8 后,F# 查询表达式和带有 Array.Contains 的 C# LINQ 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77601362/

相关文章:

unit-testing - 如何在 F# 中处理单元测试?

f# - 需要澄清有关 Microsoft.FSharp.Data.TypeProviders

f# - Topshelf.Fsharp 安装命令不执行任何操作

c# - ImageSourceConverter 抛出 NullReferenceException ... 为什么?

c# - json.NET 反序列化 2D 数组时抛出 InvalidCastException

sql - 选择具有最大总值 SQL Server 的行

php - PHP : Update DB Parse Error

c# - 如何修复 "The source contains no DataRows"?

c# - 返回一个泄露实现细节的委托(delegate)

mysql - 一年中每个月的 SUM 数据