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
For future reference, have fixed this by using:
It's the intermediary list that makes it work.
Use this:
For more: http://msdn.microsoft.com/en-us/library/dd487127.aspx
Linq dont know what ToString() Method is, however you can define your custom method. If you use this link, you get your problem solved.
You're getting that error because your LINQ to SQL doesn't recognize the
ToString()
method.If you want to convert an
int
to astring
and you're using SQL Server with EF, use the function SqlFunctions.StringConvert in this way:or this, as alternative:
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:
Be careful!
In the case you're not using SQL Server as provider, use an explicit cast in your query, as suggested by @SimonBelanger:
try this
I stumbled upon this question and wanted to post a solution that will work with Entity Framework and LINQ to entities via Lambda expression.
which was borrowed from the solution here --> https://stackoverflow.com/a/21234785/2395030