1

I get an error (below) and I cant seem to convert my providerId to string

LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression 

The resaon I am trying to convert this is because jQuery Datatables is throwing an error:

DataTables warning: table id=example2 - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4

Code:

var allProviders = _db.CareProviders.AsQueryable();
var resultItems = allProviders.Select(a => new
{
    ProviderId = Convert.ToString(a.ProviderId), //convert to string
    CareServiceType = a.CareServiceType,
    CareServiceName = a.CareServiceName,
    Email = a.Email
}).ToList();
G Gr
  • 6,030
  • 20
  • 91
  • 184
  • 1
    why you want to convert `ProviderId` to string? – Hamid Pourjam Sep 12 '15 at 18:13
  • Post the code that actually produces the error. There is no ToString() in the question now. Also we can't see what type a.Providerid is, or how it relates to the jquery part. – H H Sep 12 '15 at 18:45

1 Answers1

4

well, one typical solution would be to fetch the data from the database as-it-is and then translate it on your side:

    var allProviders = _db.CareProviders.AsQueryable();
    var resultItems = allProviders.Select(a => new
    {
        ProviderId = a.ProviderId, // fetch it as it is
        CareServiceType = a.CareServiceType,
        CareServiceName = a.CareServiceName,
        Email = a.Email
    })
    // download results from DB and execute
    // rest of the query on the application side
    .AsEnumerable()
    // now convert
    .Select(a => new
    {
        ProviderId = a.ProviderId.ToString(), //convert to string
        CareServiceType = a.CareServiceType,
        CareServiceName = a.CareServiceName,
        Email = a.Email
    });

However, sometimes this is somewhat an overkill just to convert to string..

There are some conversion methods that actually usually are understood by LINQ, but that varies dependig on your linq provider. You can experiment and try one of:

... = SqlFunctions.StringConvert(a.ProviderId)
... = Convert.ToString(a.ProviderId)
... = a.ProviderId + ""
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107