.NET 6 EF Core LINQ列表字符串包含查询报错求助
LINQ查询在.NET 6中无法翻译的问题及解决过程
以下代码在ASP.NET MVC 5中可正常执行:
var searchs = new List<string> { "a1", "a2", "a3" }; var result = db.Products.Where(p => searchs.Any(s => p.Content.Contains(s))).ToList();
(其中Content为字符串类型)
但升级至VS2022的ASP.NET Core MVC(.NET 6)后,执行该查询抛出如下异常:
System.InvalidOperationException: 'The LINQ expression 's => EntityShaperExpression: Website.Models.Product ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False .Content.Contains(s)' 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.'
尝试多种解决方案均未解决,特此求助。
更新记录
- 2023年7月17日:发现EF 8.0官方文档中存在类似示例代码
- 2023年12月1日:升级至.NET 8后,该报错不再出现
内容的提问来源于stack exchange,提问作者Tam
相关产品推荐
相关产品推荐

