C# 查询列表中的 where 子句

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

我正在尝试使用基于值列表的 where 子句来限制通过存储库返回的数据。

我有一个PurchaseOrders 实体,我想在其中返回OrderStatus 为“Open”或“Invoiced”的所有采购订单。 我将这些参数作为名为purchaseOrderParams.orderStatus 的字符串数组传递。 这些值将根据用户在客户端应用程序中的选择而变化。

如果我正在编写 SQL,它看起来会像这样。

SELECT ALL FROM PurchaseOrders WHERE OrderStaus IN ('Open', 'Invoiced')

现在我明白我需要将purchaseOrderParams.orderStatus的字符串数组转换为列表

List<string> orderStatusList = new(purchaseOrderParams.OrderStatus);

我正在尝试将其传递给查询

var query = context.PurchaseOrders
            .Include(x => x.Supplier)
            .AsQueryable();
query = query.Where(x => x.OrderStatus!.Contains(orderStatusList));

我的 where 子句似乎失败了。 参数 1:无法从 'System.Collections.Generic.List' 转换为 'char'

有什么想法吗?

我尝试改编一系列不同类型的示例,其中包括 contains 和 any,但我无法弄清楚。

c# linq lambda contains any
1个回答
0
投票

应该是

orderStatusList.Contains(x.OrderStatus)
检查订单状态列表中是否存在状态值。

var query = context.PurchaseOrders
            .Include(x => x.Supplier)
            .AsQueryable();
query = query.Where(x => orderStatusList.Contains(x.OrderStatus));
© www.soinside.com 2019 - 2024. All rights reserved.