Hoody
Hoody

Reputation: 3381

Linq IN Operator

I've tried searching for this but couldn't find examples that suited my situation.

I have this method for returning customers. How can I use the string array of codes to filter it? Contains doesn't work for me.

public static List<Customer> GetCustomers(string[] customerCodesArray)
{
    using (busDataContext g = new busDataContext())
    {
        return g.Customers.Where(
            x => x.customerCode.Contains(customerCodesArray)).ToList();
    }
}

Upvotes: 31

Views: 48930

Answers (5)

Glory Raj
Glory Raj

Reputation: 17701

Try the following code:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList(); 

Upvotes: 43

Jamie R Rytlewski
Jamie R Rytlewski

Reputation: 1221

Try

return g.Customers.Where(x=>customerCodesArray.Contains(x.CustomerCode)).ToList();

Upvotes: 5

thumbmunkeys
thumbmunkeys

Reputation: 20764

I guess this is what you want

    return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 839234

I think you need to reverse the Contains expression because you want to see if the array contains the customer code, not the other way around.

Try this:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();

Upvotes: 11

Joe
Joe

Reputation: 82654

You are backwards:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();

Upvotes: 21

Related Questions