LINQ to Entities 无法识别转换为字符串
LINQ to Entities does not recognize convert to string
我收到一个错误(如下),我似乎无法将我的 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();
嗯,一种典型的解决方案是按原样从数据库中获取数据,然后在您这边进行翻译:
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 + ""
我收到一个错误(如下),我似乎无法将我的 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();
嗯,一种典型的解决方案是按原样从数据库中获取数据,然后在您这边进行翻译:
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 + ""