1

I'm still new to modern styles .NET development and Entity Framework. I'm trying to get a list of objects where one of the values falls in a list of other values, and I'm trying to use the LINQ query methods to do so.

string cb_orderstatus = Request.Query["OrderStatusSearch"].ToString();
if (!string.IsNullOrEmpty(cb_orderstatus))
{
    string[] orderStatuses = cb_orderstatus.Split(",");
    query = query.Where(o => orderStatuses.Contains(o.Status.ToString()));
}

If the value of the cb_orderstatus is a string array containing 5, 10, and 15, I want the query to return objects where their Status equals any of these values. Currently it is not working. Can anyone help?

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
Mike Payne
  • 87
  • 2
  • 10
  • 1
    What type is `o.Status`? – Canica Nov 27 '19 at 18:09
  • and what is `query`? – Casey Crookston Nov 27 '19 at 18:10
  • Also, what does it mean not working? Does it throw an exception, what kind of exception or what kind of result it returns? – OlegI Nov 27 '19 at 18:12
  • 1
    Your code appears to be logically correct. My educated guess is that the result of `o.Status.ToString()` is not what you expect or if it is, there is a problem with the string comparison. – JuanR Nov 27 '19 at 18:13
  • If `Request.Query["OrderStatusSearch"]` is an array then the result of `ToString` is not going to be a comma separated list of the values. It's going to be the type of the array like `"System.Int32[]"` for a `int[]` – juharr Nov 27 '19 at 18:14
  • if you could provide us with code we can reproduce, that would be helpful – Casey Crookston Nov 27 '19 at 18:15
  • I was hired to edit existing code, so there is a lot of code that I am dealing with that I did not write. I'm doing my best to understand it. o.Status is an OrderStatus enum. query is an IQueryable defined earlier in the code as: ``` var query = _db.Orders.AsNoTracking(); ``` _db is a DbContext. The URL is being requested with Search?OrderStatus=0&OrderStatusSearch2=10 and I have a property on the model class that has [BindProperty] on it and is of type List but it is not being populated with any data. – Mike Payne Nov 27 '19 at 18:46
  • There were line breaks in the above reply but they're not being properly displayed. :( I'm new to posting on StackOverflow and I haven't had the easiest time learning it, especially since it keeps telling me that I am banned from posting because my questions "have not been well received by the community." – Mike Payne Nov 27 '19 at 18:48

1 Answers1

2

It is an educated guess, but as you say that

o.Status is an OrderStatus enum

then most probably you need to convert values from cb_orderstatus to actual OrderStatuses values and use OrderStatus[].Contains in the query

string cb_orderstatus = Request.Query["OrderStatusSearch"].ToString();
if (!string.IsNullOrEmpty(cb_orderstatus))
{
    var orderStatuses = cb_orderstatus
        .Split(",")
        .Select(statusIntegerString => 
            (OrderStatus)int.Parse(statusIntegerString))
        .ToArray();
    query = query.Where(o => orderStatuses.Contains(o.Status));
}

Though I am not sure that you will get comma-separated values from Request.Query["OrderStatusSearch"].

In any case, it would be orders of magnitude better to rely on standard parameter binding, so I'd recommend you to post another question that deals with

I have a property on the model class that has [BindProperty] on it and is of type List but it is not being populated with any data.

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53