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

标签 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())
    {
        try
        {
            customerList = db.Customer.Where(c => (c.customerName == "test")).ToList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    return customerList;

谁能看出为什么它在 linqpad 中有效但在 visual studio 中无效?

最佳答案

你能不能这样试试

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

因为区分大小写搜索客户名称可能存在问题。

尝试其他变体:String.Compare Method根据您的需要

关于c# - 对 varchar 字段的 linq 查询不返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950709/

相关文章:

c# - 性能,Java 泛型

c# - linq在列表中查找我的对象在哪个位置

c# - 如何从 lambda 表达式中获取唯一字符串

c++ - "STD_OUTPUT_HANDLE"标识符的第一个实例未定义

c - 一个做一个 nmake 的 visual studio 解决方案

c# - 如何在 TransactionScope 的流程中显式启动事务?

c# - 带有 resultselector 重载的 Linq GroupBy 方法有什么用?

c# - 我应该如何在启动时在 ConfigurationServices ASP.NET Core 2.0 中使用 appsettings.json 配置键/值?

c# - 如何使用 LINQ 获取序列中除最后一个元素之外的所有元素?

visual-studio - 从 Visual Studio CLI 构建 SSAS 2016