c# - Linq 选择列表中存在的对象(A,B,C)

标签 c# linq list linq-to-objects exists

我有一个订单列表。
我想根据一组订单状态选择订单

所以本质上选择 order.StatusCode in ("A", "B", "C") 中的订单

// Filter the orders based on the order status
var filteredOrders = from order in orders.Order
                     where order.StatusCode.????????("A", "B", "C")
                     select order;

最佳答案

你的状态代码也是一个集合,所以使用 Contains :

var allowedStatus = new[]{ "A", "B", "C" };
var filteredOrders = orders.Order.Where(o => allowedStatus.Contains(o.StatusCode));

或在查询语法中:

var filteredOrders = from order in orders.Order
                     where allowedStatus.Contains(order.StatusCode)
                     select order;

关于c# - Linq 选择列表中存在的对象(A,B,C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14257360/

相关文章:

c# - foreach 循环在具有多条记录的 Linq 查询后仅返回第一条记录

c# - 如何使用自定义 Controller 方法或 ASP.net MVC 中的任何其他方式将数据插入数据库?

c# - 网站 |表单认证 |获取所有登录用户(所有用户列表或计数)

c++ - 维护一个有序的(通过参数,int)集合

c# - 在遵循文档示例时(但使用异步方法),NSubstitute 模棱两可的调用

c# - 扩展方法和编译时检查

c# - 用其总和替换集合中的重复值

asp.net - 匿名类型列表

python - 将列表设置为 Pandas 数据框列中的值

python - 类型错误 : 'float' object is not subscriptable