在LINQ中将int转换为string进行搜索

7
我希望用户能够通过部分字符串搜索采购订单(INT类型),例如,如果采购订单为123456,则在搜索456时结果会返回123456。
我认为以下代码可以实现此功能:
        var pop = (from po in ctx.PurchaseOrders
               let poid = po.PurchaseOrderID.ToString()
               where poid.Contains(term)
               select new SearchItem
               {
                   id = poid,
                   label = po.SupplierID,
                   category = "purchaseorder"
               }).ToList();

但我收到了一个错误。
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

我该如何将整数类型的PurchaseOrderID转换为字符串类型,以便于我可以搜索其中的片段?

谢谢!


4
尝试直接转换,例如 let poid = (string)po.PurchaseOrderID。Linq to entities 会遵循某些方法调用,但 ToString 不在其范围内。直接转换在逻辑上相当于 SQL 中的 CAST(X as Type) - Simon Belanger
我在这里回答了类似的问题。看看吧。http://stackoverflow.com/questions/21233023/how-to-convert-following-into-linq-query-or-object-query/ - samar
你的问题的解决方案在这里:https://dev59.com/knNA5IYBdhLWcg3wKai3 - StevieB
6个回答

15

4
这会起作用。但它会在代码和后端数据库之间引入依赖关系(如果 EF 提供程序是 Sql Server,则 SqlFunctions 将起作用),这可能是个问题,也可能不是。 - Simon Belanger
Bappi,你的意思是让poid = SqlFunctions.StringConvert((double)po.PurchaseOrderID)吗?如果是的话,我会得到一个错误:LINQ to Entities不认识方法'System.String StringConvert(System.Nullable`1[System.Double])',而且这个方法无法被转换成存储表达式。 - Gordon Copestake

5

为了以后的参考,我使用如下解决方案:

    var pop = ctx
    .PurchaseOrders
    .OrderBy(x => x.PurchaseOrderID)
    .ToList()
    .Select(x => new SearchItem()
    {
        id = x.PurchaseOrderID.ToString(),
        label = x.SupplierID,
        category = "purchaseorder"
    });

是中间列表使其工作。


2
我一直在使用SqlFunctions.StringConvert,但我真的不喜欢创建依赖关系。感谢您提供的解决方案! - Ivo Coumans
1
ToList将从数据库中获取并排序所有采购订单,并将它们放入内存。如果您有大量记录,您将遇到性能问题。在检索记录之前,应该在数据库端过滤记录。如@Alberto Solano所述,使用Convert.ToString(po.PurchaseOrderID)。Contains(term)。 - user2233219

2
我偶然发现了这个问题,并想发布一个解决方案,该方案适用于使用实体框架和通过Lambda表达式使用LINQ到实体的情况。
 db.PurchaseOrders.AsEnumerable()
 .Where(x => x.PurchaseOrderID.ToString().Contains(term))
 .ToList();

这段代码参考了这里的解决方案--> https://dev59.com/knNA5IYBdhLWcg3wKai3#21234785


2
您遇到了这个错误,是因为您的LINQ to SQL不认识ToString()方法。
如果您想要将int转换为string,并且正在使用EF与SQL Server,请使用以下方式调用SqlFunctions.StringConvert函数:
let poid = SqlFunctions.StringConvert((double)po.PurchaseOrderID);

这个也是一个替代方案:
where Convert.ToString(po.PurchaseOrderID).Contains(term)

问题在于我们不知道您使用的是EF的哪个提供程序。对于SQL Server,这将起作用,但如果提供程序不同,例如MySQL,则使用该表达式,应用程序将抛出此消息的异常:

类型“System.Data.Objects.SqlClient.SqlFunctions”的指定方法System.String StringConvert无法转换为LINQ to Entities存储表达式。

要小心!

如果您没有使用SQL Server作为提供程序,请在查询中使用显式转换,如@SimonBelanger建议的那样:

let poid = (string)po.PurchaseOrderID

我得到了以下错误信息:LINQ to Entities 不认识 'System.String StringConvert(System.Nullable`1[System.Double])' 方法,该方法无法转换为存储表达式。 - Gordon Copestake
你在EF中使用哪个提供程序?我更新了我的答案。 - Alberto Solano
我正在使用System.Data.SqlClient。 - Gordon Copestake
@GordonCopestake 已更新答案。 - Alberto Solano

0

试一下这个

var pop = (from po in ctx.PurchaseOrders
               let poid = SqlFunctions.StringConvert((double)po.PurchaseOrderID)
               where poid.Contains(term)
               select new SearchItem
               {
                   id = poid,
                   label = po.SupplierID,
                   category = "purchaseorder"
               }).ToList();

1
这给了我一个错误提示:LINQ to Entities 不识别 'System.String StringConvert(System.Nullable`1[System.Double])' 方法,该方法无法转换为存储表达式。 - Gordon Copestake

0

Linq不知道ToString()方法是什么,但是你可以定义自己的方法。如果你使用这个链接, 你就可以解决你的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接