Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top