[英]Error - LINQ to Entities does not recognize the method 'System.String ToString(System.IFormatProvider)'
I am converting MVC3 linq-to-sql to MVC5 Entity Framework 6.1; 我将MVC3 linq-to-sql转换为MVC5 Entity Framework 6.1; I used the following helper for dropdown list:
我为下拉列表使用了以下帮助器:
public static string ExDropDownList (this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, bool readOnly, object htmlAttributes)
{
string html = "";
if (readOnly)
{
foreach (SelectListItem item in selectList)
{
if (item.Selected)
{
html = String.Format("<label for='{0}'>{1}</label>", name, item.Text);
break;
}
}
}
else
html = AddEmptyOption(System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, selectList, htmlAttributes).ToString());
return html;
}
I generate this list using: 我使用以下方法生成此列表:
var proviences = lookupRepository.GetProviences();
IEnumerable<SelectListItem> selectListProvience = from p in proviences
select new SelectListItem
{
Text = p.ProvinceName,
Value = p.ProvinceID.ToString(CultureInfo.InvariantCulture)
};
I get the following error: 我收到以下错误:
LINQ to Entities does not recognize the method 'System.String ToString(System.IFormatProvider)' method, and this method cannot be translated into a store expression
Entity Framework is trying to convert this to SQL, which fails because it can't handle ToString()
. 实体框架正在尝试将此转换为SQL,但失败了,因为它无法处理
ToString()
。 Try adding a ToList()
instead: 尝试添加
ToList()
代替:
var proviences = lookupRepository.GetProviences().ToList();
This causes the query to execute, but this is fine if you're selecting every item in the list anyway. 这将导致查询执行,但是如果您仍要选择列表中的每个项目,这都很好。
You need to materialise the query before you invoke ToString(). 在调用ToString()之前,需要实现查询。
var proviences = lookupRepository.GetProviences();
IEnumerable<SelectListItem> selectListProvience = (from p in proviences).AsEnumerable()
.Select(p => new SelectListItem
{
Text = p.ProvinceName,
....
but it could be just 但这可能只是
var proviences = lookupRepository.GetProviences();
IEnumerable<SelectListItem> selectListProvience = proviences.Select(p => new SelectListItem
{
Text = p.ProvinceName,
or easier still 还是更容易
SeelctList selectListProvience = new SelectList(proviences, "ProvinceID", "ProvinceName");
I had same issue when separated my model project I removed using System.Data.Objects.SqlClient;
当分离
using System.Data.Objects.SqlClient;
删除的模型项目时,我遇到了相同的问题using System.Data.Objects.SqlClient;
and replaced it with using System.Data.Entity.SqlServer;
并
using System.Data.Entity.SqlServer;
替换它using System.Data.Entity.SqlServer;
and issue was gone 问题不见了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.