对varchar字段的linq查询不返回任何结果

use*_*698 7 c# linq linqpad visual-studio

当我在linqpad中运行此查询时:

Customer.Where(c => (c.CustomerName == "test"))
Run Code Online (Sandbox Code Playgroud)

它返回匹配的记录.

当我尝试在visual studio中运行相同的查询时,它不会返回任何匹配的记录.这是我正在使用的代码:

    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;
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到为什么它在linqpad中工作但在visual studio中不起作用?

Pra*_*ana 2

你可以尝试这样吗

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

因为区分大小写搜索客户名称可能会出现问题。

根据需要尝试String.Compare 方法的其他变体