Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在linq中将int转换为字符串以进行搜索_C#_Asp.net_Linq_Entity Framework - Fatal编程技术网

C# 在linq中将int转换为字符串以进行搜索

C# 在linq中将int转换为字符串以进行搜索,c#,asp.net,linq,entity-framework,C#,Asp.net,Linq,Entity Framework,我希望我的用户能够通过部分字符串搜索采购订单(整数),也就是说,如果采购订单是123456,键入456将在结果中显示123456 我认为这会奏效: var pop = (from po in ctx.PurchaseOrders let poid = po.PurchaseOrderID.ToString() where poid.Contains(term) select new Sear

我希望我的用户能够通过部分字符串搜索采购订单(整数),也就是说,如果采购订单是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.
如何将INT-PurchaseOrderID转换为字符串,以便搜索其片段


谢谢,Linq不知道什么是ToString()方法,但是您可以定义自定义方法。如果您使用,您的问题就会得到解决。

使用以下方法:

id = SqlFunctions.StringConvert((double)poid)
更多信息:

试试这个

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();

之所以出现此错误,是因为LINQ to SQL无法识别
ToString()
方法

如果要将
int
转换为
string
,并且要将SQL Server与EF一起使用,请按以下方式使用该函数:

let poid = SqlFunctions.StringConvert((double)po.PurchaseOrderID);
或者,作为替代方案:

where Convert.ToString(po.PurchaseOrderID).Contains(term)
问题是,我们不知道您使用的EF提供程序是什么。对于SQL Server,这将起作用,但如果提供程序与使用该表达式的MySQL不同,则应用程序将抛出一个异常消息:

类型上的指定方法System.String StringConvert 无法将“System.Data.Objects.SqlClient.SqlFunctions”转换为 LINQ到实体存储表达式

 db.PurchaseOrders.AsEnumerable()
 .Where(x => x.PurchaseOrderID.ToString().Contains(term))
 .ToList();
小心

如果不使用SQL Server作为提供程序,请在查询中使用显式强制转换,如@SimonBelanger所建议的:

let poid = (string)po.PurchaseOrderID

为了便于将来参考,我们已通过使用以下方法修复了此问题:

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

正是中介列表使其发挥了作用。

我偶然发现了这个问题,想发布一个解决方案,该解决方案将通过Lambda表达式与实体框架和LINQ to entities一起工作

 db.PurchaseOrders.AsEnumerable()
 .Where(x => x.PurchaseOrderID.ToString().Contains(term))
 .ToList();

这是从这里的解决方案中借用的-->

尝试直接转换,即
让poid=(string)po.PurchaseOrderID
。Linq to实体将执行某些方法调用,
ToString
不是其中之一。直接转换在逻辑上等同于SQL中的
cast(X as Type)
。我在这里回答了类似的问题。过来看。你的问题的解决方案就在这里:这会奏效的。但是它在代码和后端数据库之间引入了依赖关系(如果EF提供程序是Sql Server,则SqlFunctions将起作用),这可能是个问题,也可能不是。Bappi,您的意思是让poid=SqlFunctions.StringConvert((double)po.PurchaseOrderID)如果是这样,我会得到一个错误LINQ to Entities不识别方法'System.String StringConvert'(System.Nullable`1[System.Double])方法,此方法无法转换为存储表达式。这说明:LINQ to Entities无法识别方法'System.String StringConvert(System.Nullable`1[System.Double])'方法,此方法无法转换为存储表达式。我得到:LINQ to Entities无法识别方法'System.String StringConvert(System.Nullable`1[System.Double])'方法,此方法无法转换为存储表达式。您在EF中使用的是哪个提供程序?我更新了答案。我使用的是System.Data。SqlClient@GordonCopestake更新答案。我一直在使用SqlFunctions.StringConvert,但我真的不喜欢创建依赖项。感谢您提供此解决方案!ToList将订购和从数据库获取所有采购订单并将其放入内存。如果您有大量记录,则会遇到性能问题。在检索记录之前,您应该在数据库端筛选记录。使用Convert.ToString(po.PurchaseOrderID)。包含@Alberto Solano所述的(术语)。