我收到错误(下面),我似乎无法将我的providerId转换为字符串
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression
我试图转换它的原因是因为jQuery Datatables引发了一个错误:
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
代码:
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();
答案 0 :(得分:2)
好吧,一个典型的解决方案是从数据库中获取数据,然后将其翻译成您的一面:
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
});
但是,有时这只是一种过度杀伤只是为了转换为字符串..
LINQ实际上通常会理解一些转换方法,但这取决于您的linq提供程序。您可以尝试并尝试以下方法之一:
... = SqlFunctions.StringConvert(a.ProviderId)
... = Convert.ToString(a.ProviderId)
... = a.ProviderId + ""