【问题标题】:How can I write this query in Linq to Entities extension method as a where clause?如何在 Linq to Entities 扩展方法中将此查询编写为 where 子句?
【发布时间】:2013-06-28 22:39:27
【问题描述】:

给定以下 SQL 语句:

Select * From Customers Where ID LIKE ('%160%')

如果 ID 是一个 int 字段,我如何使用 Linq to Entities 扩展方法来做到这一点?

对于字符串字段,我可以这样做...

filterExpression = "ID.Contains(\"160\")";
IQueryable<Customer> c = context.Customers.Where(filterExpression);

当我尝试这个时,我得到了错误:

No applicable method 'Contains' exists in type 'Int32'

我一直在寻找一个小时来了解如何让它正常工作。

编辑:修改以使代码更具可读性。

【问题讨论】:

    标签: linq-to-entities extension-methods where-clause


    【解决方案1】:

    无法测试 atm,但我会试试这个;

    var myExpression = x => SqlFunctions.StringConvert((double)x.ID).Contains("160");
    IQueryable<Customer> c = context.Customers.Where(myExpression);
    

    【讨论】:

    • 给出错误:“客户”类型中不存在属性或字段“SqlFunctions”
    • @Scottie Hm,似乎在错误的地方寻找System.Data.Objects.SqlClient.SqlFunctions。你能试试编辑吗?
    • 由于我最终使用 StringConvert 函数完成了与您在此处发布的内容非常相似的操作,因此我将您的标记为正确答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多