Computer
Computer

Reputation: 2227

Possible to limit returned results by multiple values

I'm a little miffed with this one, i have a list of customers coming from a query:

IQueryable<Customer> Customers()
{
 return myDc.Customers;
}

This returns all the customers.

The Customers table has two additional columns, Risk and Status. Risk tells us what risk we have with that customer and Status tells us what state their account is under.

The user has a search page where they select multiple Risk and Statuses. I capture their selected values into a List of integers and would like to pass that into my query above.

Since the user can select multiple Risk values (and Statuses) if i have a foreach around the Customers query it doesnt get all the Risks the user selected i.e.

foreach (int r in RisksList)
{
  return Customers.Where(r=> r.Risk.Tostring().Contains(r.ToString());
}

When the above query executes it limits it to the first Risk found, but any other Risks contained within RisksList is not captured.

I would be doing the same with Statuses (once i have the correct Risks records).

How could i limit all the customers found by the integer values contained within RisksList (and then limit the StatusList)?

Edit 1

    IQueryable<Customer> customers = from c in myDc.Customers select c;

    foreach (int i in RisksList)
    {
        customers = from cc in customers where (RisksList.Contains(i)) select cc;
    }

Upvotes: 0

Views: 52

Answers (2)

Panda
Panda

Reputation: 458

When the above query executes it limits it to the first Risk found, but any other Risks contained within RisksList is not captured.

That's because of your return statement inside your loop.

Instead of returning the results, you can add it to a result list, or you could try that cleaner way :

return Customers.Where(r=> RiskLists.Contains(r.Risk));

The same for your other lists.

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16956

Remove foreach and just use this.

return Customers.Where(r=> RisksList.Any(x=> r.Risk.ToString().Contains(x.ToString()));

Upvotes: 1

Related Questions