Linq select objects in list where exists IN (A,B,C)
I have a list of orders
.
I want to select orders
based on a set of order statuses.
So essentially select orders where 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;
Solution 1:
Your status-codes are also a collection, so use Contains
:
var allowedStatus = new[]{ "A", "B", "C" };
var filteredOrders = orders.Order.Where(o => allowedStatus.Contains(o.StatusCode));
or in query syntax:
var filteredOrders = from order in orders.Order
where allowedStatus.Contains(order.StatusCode)
select order;
Solution 2:
var statuses = new[] { "A", "B", "C" };
var filteredOrders = from order in orders.Order
where statuses.Contains(order.StatusCode)
select order;
Solution 3:
Try with Contains
function;
Determines whether a sequence contains a specified element.
var allowedStatus = new[]{ "A", "B", "C" };
var filteredOrders = orders.Order.Where(o => allowedStatus.Contains(o.StatusCode));