7

When I run this query in linqpad:

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

It returns a record that matches.

When I try running the same query in visual studio it does not return any matching records. This is the code I am 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 see why this it works in linqpad but does not work in visual studio?

svick
  • 236,525
  • 50
  • 385
  • 514
user1696698
  • 219
  • 3
  • 12
  • If you debug, does the `db.Customer` object have a bunch of objects in them with at least one with "test" as the `customerName`? It looks straight forward enough for that to be the easiest issue. – Amadiere Dec 19 '12 at 10:54
  • You have different case of ``customerName`` in your VS code. I don't know if that is just a typo here. Also, the ``new List()`` assignment is redundant because it will get overwritten by the query. – Gordon Leigh Dec 19 '12 at 10:56
  • linqpad capitalises the object name, whereas VS does not change it. – user1696698 Dec 19 '12 at 11:12

2 Answers2

2

can you try out like this

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

because there may be problem with the Casesensitive search for customer name.

Try other variation of : String.Compare Method as per you need

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
0

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

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

EDIT: DeeMac has also covered this in his reply

jdtaylor
  • 334
  • 2
  • 5
  • 20