Linq query for varchar field returns no results

When I run this query in linqpad :

Customer.Where(c => (c.CustomerName == "test")) 

It returns a record that matches.

When I try to run the same query in visual studio , it does not return any matching records. This is the code I'm using:

  List<Customer> customerList = new List<Customer>(); using (DBEntities db = new DBEntities()) { try { customerList = db.Customer.Where(c => (c.customerName == "test")).ToList(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } return customerList; 

Can anyone understand why this works in linqpad but does not work in visual studio ?

+7
source share
2 answers

You can try like this

 customerList = db.Customer. Where(c => String.Compare (c.customerName.ToUpper(),"test".ToUpper()) == 0).ToList(); 

because there may be a problem with search on demand to find customers.

Try other options: String.Compare Method as you wish

+2
source

your linqpad request uses "c.CustomerName" and your visual studio request uses "c.customerName".

Alternatively, your problem may be that it is case sensitive or the db.Customer set is empty.

EDIT: DeeMac also covered this in his answer

0
source

All Articles