-1

I am trying to limit the data returned via a repository using the where clause based on a list of values.

I have a PurchaseOrders entity where I want to return all purchase orders that have an OrderStatus of either "Open" or "Invoiced". I am passing in those parameters as a string array named purchaseOrderParams.orderStatus. These values will change based on the user selection in the client app.

If I was writing an SQL it would look like:

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

Now I understand that I need to convert the string array of purchaseOrderParams.orderStatus to a list:

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

And I am trying to pass it to the query:

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

My where clause appears to be failing.

Argument 1: cannot convert from 'System.Collections.Generic.List' to 'char'

Any ideas?

I have tried to adapt a bunch of different types of examples that include contains and any but I can't figure it out.

1
  • You have to flip the values: list.Contains(value) Commented Jul 6 at 6:43

1 Answer 1

4

It should be orderStatusList.Contains(x.OrderStatus) that checks whether the status value exists in the order status list.

var query = context.PurchaseOrders
            .Include(x => x.Supplier)
            .AsQueryable();
query = query.Where(x => orderStatusList.Contains(x.OrderStatus));
1
  • Thanks mate. I feel like an idiot. It's so obvious.
    – michael
    Commented Jul 6 at 7:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.