英文:
EF Core 7: .Any() in query filter produce "could not be translated. Either rewrite the query in a form that can be translated"
问题
我使用了相当简单的实体 Request(见下面的示例)和自定义查询过滤器 ByStatuses 作为扩展。当我使用 .ToArrayAsync() 时,我得到了一个异常 "无法翻译。请以可翻译的形式重写查询"。
public class Request
{
public virtual RequestStatus Status { get; set; }
public int? StatusId { get; set; }
}
public static IQueryable<Request> ByStatuses(this IQueryable<Request> query, IEnumerable<int> ids)
{
return query.Where(o => ids.Any(oo => o.StatusId == oo));
}
var local = await query.ToArrayAsync();
如果我更改为这样
return query.Where(o => o.StatusId != null && ids.Any(oo => o.StatusId == oo));
并且对上下文选项使用 .UseRelationalNulls() - 这不会改变任何东西。
在 EF 7 中,"Any" 和 "All" 操作符支持得不好吗?或者它们不能在复杂的查询链中使用吗?或者这是表行中 null 属性的问题?
错误:
LINQ 表达式 'oo => EntityShaperExpression:
X5.TC5.TC.Domain.Entities.Request
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.StatusId == (int?)oo' 无法翻译。请以可翻译的形式重写查询,或者通过插入对 'AsEnumerable'、'AsAsyncEnumerable'、'ToList' 或 'ToListAsync' 的调用明确地切换到客户端评估。有关更多信息,请参见 https://go.microsoft.com/fwlink/?linkid=2101038。
英文:
I use pretty simple entity Request (see example below) and custom query filter ByStatuses as extension. When I use .ToArrayAsync() I got an exception "could not be translated. Either rewrite the query in a form that can be translated"
public class Request
{
public virtual RequestStatus Status { get; set; }
public int? StatusId { get; set; }
}
public static IQueryable<Request> ByStatuses(this IQueryable<Request> query, IEnumerable<int> ids)
{
return query.Where(o => ids.Any(oo => o.StatusId == oo));
}
var local = await query.ToArrayAsync();
If I change to this
return query.Where(o => o.StatusId != null && ids.Any(oo => o.StatusId == oo));
and use .UseRelationalNulls() for context options - this doesnt change anything.
Are "Any" and "All" operators doesnt supported in EF 7 pretty well? Or they cant be used in complex query chains? Or this is problem with null property in table row?
Error:
The LINQ expression 'oo => EntityShaperExpression:
X5.TC5.TC.Domain.Entities.Request
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
.StatusId == (int?)oo' 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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
答案1
得分: 0
这实际上是 EF Core 7.0 中的一个错误。链接:https://github.com/dotnet/efcore/issues/31081
英文:
As it turns out this is a bug in EF Core 7.0 https://github.com/dotnet/efcore/issues/31081
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论