Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert int to string in linq for searching

I would like my users to be able to search for purchase orders (which are INT's) via partial strings, i.e. if a purchase order is 123456 typing in 456 gives me 123456 in the results.

I thought this would work:

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

But I get an error

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

How can I convert the INT PurchaseOrderID to a string to enable me to search for fragments of it?

Thanks

like image 871
Gordon Copestake Avatar asked Jan 21 '14 11:01

Gordon Copestake


People also ask

How to convert int to string in LINQ?

Text = c.Name }; var items = from c in contacts select new ListItem { Value = c. ContactId. ToString(), //Throws exception: ToString is not supported in linq to entities. Text = c.Name };

Can you use LINQ on a string?

LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String.

Is there something like LINQ in Java?

There is nothing like LINQ for Java.


4 Answers

Use this:

id = SqlFunctions.StringConvert((double)poid)

For more: http://msdn.microsoft.com/en-us/library/dd487127.aspx

like image 138
Bappi Datta Avatar answered Oct 17 '22 01:10

Bappi Datta


For future reference, have fixed this by using:

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

It's the intermediary list that makes it work.

like image 20
Gordon Copestake Avatar answered Oct 17 '22 03:10

Gordon Copestake


You're getting that error because your LINQ to SQL doesn't recognize the ToString() method.

If you want to convert an int to a string and you're using SQL Server with EF, use the function SqlFunctions.StringConvert in this way:

let poid = SqlFunctions.StringConvert((double)po.PurchaseOrderID);

or this, as alternative:

where Convert.ToString(po.PurchaseOrderID).Contains(term)

The problem is that we don't know what is the provider you're using with EF. In the case of SQL Server, this will work, but if the provider is different, like MySQL, using that expression, the application will throw an exception with this message:

The specified method System.String StringConvert on the type 'System.Data.Objects.SqlClient.SqlFunctions' cannot be translated into a LINQ to Entities store expression.

Be careful!

In the case you're not using SQL Server as provider, use an explicit cast in your query, as suggested by @SimonBelanger:

let poid = (string)po.PurchaseOrderID
like image 2
Alberto Solano Avatar answered Oct 17 '22 01:10

Alberto Solano


I stumbled upon this question and wanted to post a solution that will work with Entity Framework and LINQ to entities via Lambda expression.

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

which was borrowed from the solution here --> https://stackoverflow.com/a/21234785/2395030

like image 2
ejhost Avatar answered Oct 17 '22 01:10

ejhost