By MartinS


2013-01-10 11:53:33 8 Comments

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;

5 comments

@Balvinder Singh 2018-06-19 00:19:22

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

@Jay Shah 2018-11-09 18:11:12

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

@Alexander Christov 2015-07-26 15:58:51

NB: this is LINQ to objects, I am not 100% sure if it work 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.

@Tim Schmelter 2013-01-10 11:55:06

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;

@Jay Shah 2018-11-09 18:14:00

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" };

@Soner Gönül 2013-01-10 11:56:05

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

@user439931 2013-01-10 11:55:21

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

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

Related Questions

Sponsored Content

45 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

26 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

28 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2551047 View
  • 3235 Score
  • 28 Answer
  • Tags:   python list

13 Answered Questions

[SOLVED] How to randomly select an item from a list?

  • 2008-11-20 18:42:21
  • Ray Vega
  • 1318939 View
  • 1661 Score
  • 13 Answer
  • Tags:   python list random

16 Answered Questions

[SOLVED] How to clone or copy a list?

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3534283 View
  • 2895 Score
  • 29 Answer
  • Tags:   python list indexing

20 Answered Questions

7 Answered Questions

[SOLVED] Multiple "order by" in LINQ

  • 2008-11-18 13:34:11
  • Sasha
  • 578221 View
  • 1539 Score
  • 7 Answer
  • Tags:   linq sql-order-by

19 Answered Questions

[SOLVED] How to Sort a List<T> by a property in the object

Sponsored Content