convert int to string in linq for searching

asp.net c# entity-framework linq

Question

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

1
7
1/21/2014 11:48:41 AM

Accepted Answer

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.

4
1/21/2014 12:40:58 PM

Popular Answer

Use this:

id = SqlFunctions.StringConvert((double)poid)

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow