最近,我将代码迁移到.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
方法.我真的需要在每个地方都添加array.Length = 0 ||
这个角色吗?或许我可以做一些更好的事情?
我使用的是SQL Server 2022.