MySql-Provider:包含在 Linq-Query 中失败

问题描述 投票:0回答:1

我正在尝试创建一个应与字符串列表上的 Contains 一起使用的 Linq 查询。

以下查询在 SqlServer 和 PostgreSQL 提供程序中运行不会出现错误:

IEnumerable<string> listStringValues = listStringValues.Where ( x => x != null ).Select ( x => x.ToUpper () );

var query = _ctx.Invoices.Where ( x => ( x.FormatType.FormatTypeCode == FormatTypeCode.ER ||
                                             x.FormatType.FormatTypeCode == FormatTypeCode.AR ||
                                             x.FormatType.FormatTypeCode == FormatTypeCode.EA ||
                                             x.FormatType.FormatTypeCode == FormatTypeCode.AA ) &&
listStringValues.Any ( y => y.Contains ( x.ExternalNumber.ToUpper () ) ) );                                 

现在我的问题是,当我将此查询与 MySql 提供程序(Pomelo.EntityFrameworkCore.MySql (8.0.2))一起使用时,会出现以下错误消息:

The LINQ expression '[Microsoft.EntityFrameworkCore.Query.ParameterQueryRootExpression]' could not be translated. Additional information: Primitive collections support has not been enabled. 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.

您对如何重构查询有什么建议,以便您不必经历首先根据 FormatTypeCode 加载所有发票,然后使用 LinqToObjects 根据 listStringValues 列表过滤它们的弯路吗?

提前感谢您的帮助!

问候 SB

我尝试拆分查询,我也尝试了这种方法,首先加载所有发票并通过 LinqToObjects 通过 Contains 执行过滤器,这有效!

c# linq-to-entities mysql-connector
1个回答
0
投票

当我尝试这个时,它也适用于 MySql:

var query = _ctx.Invoices.Where ( x => ( x.FormatType.FormatTypeCode == FormatTypeCode.ER ||
                                         x.FormatType.FormatTypeCode == FormatTypeCode.AR ||
                                         x.FormatType.FormatTypeCode == FormatTypeCode.EA ||
                                         x.FormatType.FormatTypeCode == FormatTypeCode.AA ) );

var objs = query.AsEnumerable ();

var FilteredList = objs.Where ( x => listStringValues .Any ( y => y.Contains ( x.ExternalNumber.ToUpper() ) ) ).ToList();

© www.soinside.com 2019 - 2024. All rights reserved.