Asp.net LINQ to实体无法识别方法';字符串ToString()';方法,而此方法无法转换为存储表达式

Asp.net LINQ to实体无法识别方法';字符串ToString()';方法,而此方法无法转换为存储表达式,asp.net,asp.net-mvc-3,Asp.net,Asp.net Mvc 3,这是我的控制器类 public class HomeController : Controller { private rikuEntities rk = new rikuEntities(); public ActionResult Index() { var db = new rikuEntities(); IEnumerable<SelectListItem> items = db.emp.Select(c => n

这是我的控制器类

public class HomeController : Controller
{
    private rikuEntities rk = new rikuEntities();
    public ActionResult Index()
    {
        var db = new rikuEntities();
        IEnumerable<SelectListItem> items = db.emp.Select(c => new     
               SelectListItem
               {
                 Value = c.Id.ToString(), 
                 Text = c.name
               });
        ViewBag.CategoryID = items;
        return View();
    }
}
公共类HomeController:控制器
{
私有rikuEntities rk=新的rikuEntities();
公共行动结果索引()
{
var db=新的rikuEntities();
IEnumerable items=db.emp.Select(c=>new
选择列表项
{
Value=c.Id.ToString(),
Text=c.name
});
ViewBag.CategoryID=项目;
返回视图();
}
}
这是我的看法

@using (Html.BeginForm("viewToController", "Home"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>emp</legend>

        <div class="editor-field">
            @Html.DropDownList("CategoryID", (IEnumerable<SelectListItem>) ViewBag.Categories)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
@使用(Html.BeginForm(“viewToController”、“Home”))
{
@Html.ValidationSummary(true)
电磁脉冲
@Html.DropDownList(“CategoryID”,(IEnumerable)ViewBag.Categories)

}
无论何时运行此程序,都会出现以下错误:

LINQ to Entities无法识别方法'System.String ToString()'方法,并且无法将此方法转换为存储表达式。”在语句@Html.DropDownList(“CategoryID”,(IEnumerable)ViewBag.Categories)中。我正在使用实体框架机制进行数据库连接。请帮助我找出错误


我建议您使用视图模型和强类型视图,而不是ViewBag。因此,请从定义视图模型开始:

public class EmployeeViewModel
{
    public string CategoryId { get; set; }
    public IEnumerable<Employee> Categories { get; set; }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var db = new rikuEntities();
        var model = new EmployeeViewModel
        {
            Categories = db.emp.ToArray() // <-- you probably want categories here
        };
        return View(model);
    }
}
公共类EmployeeViewModel
{
公共字符串CategoryId{get;set;}
公共IEnumerable类别{get;set;}
}
然后在控制器中填充此视图模型:

public class EmployeeViewModel
{
    public string CategoryId { get; set; }
    public IEnumerable<Employee> Categories { get; set; }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var db = new rikuEntities();
        var model = new EmployeeViewModel
        {
            Categories = db.emp.ToArray() // <-- you probably want categories here
        };
        return View(model);
    }
}
公共类HomeController:控制器
{
公共行动结果索引()
{
var db=新的rikuEntities();
var模型=新员工视图模型
{
Categories=db.emp.ToArray()//x.CategoryId,
新的选择列表(Model.Categories,“Id”,“name”)
)

}
遗憾的是,EF不知道如何将
ToString()
转换为SQL语句

因此,必须使用嵌入式函数

int
没有重载,因此需要将类型转换为
double
:-(


我遇到了同样的问题,我仍然不知道如何在表达式中执行“.ToString()”方法。它返回错误:LINQ to Entities无法识别方法“System.String ToString()”方法,并且此方法无法转换为存储表达式下面的主题中有一个很好的答案:的可能重复