简体   繁体   English

错误-LINQ to Entities无法识别方法'System.String ToString(System.IFormatProvider)'

[英]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.

相关问题 LINQ to Entities无法识别MVC5中的方法&#39;System.DateTime ParseExact(System.String,System.String,System.IFormatProvider)&#39;方法错误 - LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method error in MVC5 LINQ to Entities无法识别方法&#39;System.String ToString()&#39;方法++++++ - LINQ to Entities does not recognize the method 'System.String ToString()' method ++++++ LINQ to Entities无法识别方法&#39;System.String ToString(System.String) - LINQ to Entities does not recognize the method 'System.String ToString(System.String) LINQ to Entities无法识别方法&#39;System.String ToString(System.String)&#39; - LINQ to Entities does not recognize the method 'System.String ToString(System.String)' 查询错误:LINQ to Entities无法识别方法&#39;System.String ToString()&#39;方法 - Error in query:LINQ to Entities does not recognize the method 'System.String ToString()' method LINQ to Entities无法识别方法&#39;System.String ToString()&#39;。 尝试解析DateTime时发生错误 - LINQ to Entities does not recognize the method 'System.String ToString()'. Error happend when trying parse DateTime 错误:LINQ to Entities无法识别字符串转换时发生的方法&#39;System.String ToString(System.Object)&#39;方法 - Error: LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method occurs while string conversion LINQ to Entities无法识别方法&#39;System.String ToString(System.Object)&#39;方法错误,且字段可为空 - LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method error occurs with nullable fields 意外错误:LINQ to Entities 无法识别方法“System.String DecryptValue(Byte[], System.String)”方法 - Unexpected Error : LINQ to Entities does not recognize the method 'System.String DecryptValue(Byte[], System.String)' method LINQ to Entities无法识别方法&#39;System.String ToString()&#39;,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM