Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq IN Operator

Tags:

c#

linq-to-sql

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();     } } 
like image 711
Hoody Avatar asked Nov 07 '11 21:11

Hoody


People also ask

What is LINQ operator?

What are LINQ Operators? The LINQ Operators are nothing but a set of extension methods that are used to write LINQ Query. These LINQ extension methods provide lots of very useful features which we can apply to the data source. Some of the features are filtering the data, sorting the data, grouping the data, etc.

What is the LINQ equivalent to the SQL IN operator?

An IEnumerable<T>.

How the query operators works in LINQ?

The standard query operators provide query capabilities including filtering, projection, aggregation, sorting and more. There are two sets of LINQ standard query operators: one that operates on objects of type IEnumerable<T>, another that operates on objects of type IQueryable<T>.


1 Answers

Try the following code:

return g.Customers.Where(x => customerCodesArray.Contains(x.customerCode)).ToList();  
like image 197
Glory Raj Avatar answered Sep 21 '22 12:09

Glory Raj