1

I have search box which I want to return results based on TicketID. However, when trying to convert the TicketID to string to compare it against the search term string, I receive this error...

Here is my method:

public ActionResult Autocomplete(Ticket ticket, string term)
{
    var searchTickets = db.Tickets
        .Where(t => t.StatusID != 3 && 
            Convert.ToString(t.TicketID).StartsWith(term))
        .Take(10)
        .Select(t => new
        {
            label = t.Summary
        });

    return Json(searchTickets, JsonRequestBehavior.AllowGet);
}

I have tried other suggestions on similar posts such as the SqlFunctions.StringConvert() extension method, however, this throws a syntax error before the project is even built...

Any guidance will be appreciated.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Tomuke
  • 869
  • 2
  • 8
  • 26
  • 1
    You should use `SqlFunctions` indeed, what have you tried? – haim770 Apr 27 '14 at 14:09
  • possible duplicate of [LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method) – John Saunders Apr 27 '14 at 14:32

1 Answers1

2

try like this:

Using System.Data.Objects.SqlClient;


 var searchTickets = db.Tickets
        .Where(t => t.StatusID != 3 && 
            SqlFunctions.StringConvert((double)t.TicketID).StartsWith(term)
        .Take(10)
        .Select(t => new
        {
            label = t.Summary
        });

With EF 4 you can use SqlFunctions.StringConvert. There is no overload for int so you need to cast to a double or a decimal.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160