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

C# varchar字段的linq查询不返回任何结果,c#,linq,visual-studio,linqpad,C#,Linq,Visual Studio,Linqpad,在linqpad中运行此查询时: Customer.Where(c => (c.CustomerName == "test")) 它返回一个匹配的记录 当我尝试在visual studio中运行相同的查询时,它不会返回任何匹配的记录。这是我正在使用的代码: List<Customer> customerList = new List<Customer>(); using (DBEntities db = new DBEntities())

linqpad中运行此查询时

Customer.Where(c => (c.CustomerName == "test"))
它返回一个匹配的记录

当我尝试在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;
List customerList=new List();
使用(DBEntities db=new DBEntities())
{
尝试
{
customerList=db.Customer.Where(c=>(c.customerName==“test”)).ToList();
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
返回客户列表;

有人知道为什么它在linqpad中工作,但在visualstudio中不工作吗?

你能这样试试吗

customerList = db.Customer.
   Where(c => String.Compare (c.customerName.ToUpper(),"test".ToUpper()) == 0).ToList();
因为对客户名称的区分大小写搜索可能存在问题

尝试以下其他变体:根据需要

您的linqpad查询使用“c.CustomerName”,而visual studio查询使用“c.CustomerName”

或者,您的问题可能是它区分大小写,或者db.Customer集为空

编辑:
DeeMac在他的回复中也谈到了这一点

如果您进行调试,
db.Customer
对象中是否有一组对象,其中至少有一个对象使用“test”作为
customerName
?这是最简单的问题。在VS代码中,您有不同的
customerName
。我不知道这是否只是一个打字错误。另外,
new List()
赋值是多余的,因为它将被查询覆盖。linqpad将对象名称大写,而VS不会更改它。