236

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;
1
  • Thanks to all that answered so quickly. Esp for the lambda solution. I've not done anything with lambda expressions as yet. I assume I'd do a NOT contains using (o => !(statuses.Contains(o.OrderHeaderOrderStatusCode))) Commented Jan 10, 2013 at 12:29

5 Answers 5

375

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;
Sign up to request clarification or add additional context in comments.

3 Comments

I would say that use HashSet instead of array for allowedStatus because HashSet's contains method is fastest and there'll be performance issues with array if it contains more than 1000 items. var allowedStatus = new HashSet<string> { "A", "B", "C" };
@JayShah: late answer: yes, in general you are right, if you have thousands of items it might be better to use a HashSet<T> in the first place. But it's not a good idea always: You need another collection, so more memory, if you pass in already an array or list. You need to fill that HashSet<T>, so more cpu cycles. You might not be able to use thousands of items anyway if this is not Linq-To-Objects but Linq-To-Entities(the Contains is translated to an sql-IN clause which has limitations).
@JayShah also, this does not change the performance when using entity framework. It will be converted to the SQL "IN" clause and will be executed by the database engine. In this case, the array will be a bit faster to create.
28

NB: this is LINQ to objects, I am not 100% sure if it works in LINQ to entities, and have no time to check it right now. In fact it isn't too difficult to translate it to x in [A, B, C] but you have to check for yourself.

So, instead of Contains as a replacement of the ???? in your code you can use Any which is more LINQ-uish:

// Filter the orders based on the order status
var filteredOrders = from order in orders.Order
                     where new[] { "A", "B", "C" }.Any(s => s == order.StatusCode)
                     select order;

It's the opposite to what you know from SQL this is why it is not so obvious.

Of course, if you prefer fluent syntax here it is:

var filteredOrders = orders.Order.Where(order => new[] {"A", "B", "C"}.Any(s => s == order.StatusCode));

Here we again see one of the LINQ surprises (like Joda-speech which puts select at the end). However it is quite logical in this sense that it checks if at least one of the items (that is any) in a list (set, collection) matches a single value.

Comments

19
var statuses = new[] { "A", "B", "C" };

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

Comments

18

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));

Comments

-5

Just be careful, .Contains() will match any substring including the string that you do not expect. For eg. new[] { "A", "B", "AA" }.Contains("A") will return you both A and AA which you might not want. I have been bitten by it.

.Any() or .Exists() is safer choice

1 Comment

new[] { "B", "AA" }.Contains("A") will return false, NOT true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.