Search code examples
c#asp.netlinqentity-framework

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


Solution

  • 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.