LINQ到实体无​​法识别方法'System.String的ToString()“方法在MVC 4 [英] LINQ to Entities does not recognize the method 'System.String ToString()' method in MVC 4

查看:155
本文介绍了LINQ到实体无​​法识别方法'System.String的ToString()“方法在MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与MVC 4个工作,我必须使用code首先迁移到更新我的数据库。我想要做的就是从数据库表中选择记录,并将其插入到一个下拉列表,用户可从中选择一个。

我有一个错误,我不明白:

LINQ到实体无​​法识别方法'System.String的ToString()方法,而这种方法不能被翻译成店前pression。

控制器:

 公众的ActionResult ADDNEW()
        {
            VAR DBA =新DefaultConnection();
            VAR的查询= dba.blob.Select(C =>新建SelectListItem
            {
                值= c.id.ToString(),
                文= c.name_company,
                选定= c.id.Equals(3)
            });
            VAR模型=新Companylist
            {
                xpto = query.AsEnumerable()
            };            返回查看(模型);
        }


解决方案

您得到这个错误,因为实体框架不知道如何在SQL执行的ToString()方法。所以,你应该使用了ToList 加载数据,然后转化为SelectListItem为:

  VAR的查询= dba.blob.ToList()选择(C =方式>新SelectListItem
    {
    值= c.id.ToString(),
    文= c.name_company,
    选定= c.id.Equals(3)
});

编辑:为了更清楚,实体框架查询经营者的使用转换,如选择其中, ETC到一个SQL查询来加载数据。如果你喜欢叫 A方法ToString(),其中实体框架没有在SQL等价物,它会抱怨。这样的想法是推迟使用这些功能发布数据加载。 了ToList ,这样的查询加载数据的的ToArray ETC力执行。一旦数据被加载,任何进一步的操作(如其中, ETC选择,)使用LINQ到对象上执行,上已经在存储器中的数据。

I'm working with MVC 4 and I have to update my database using Code First Migrations. What I'm trying to do is to select records from a database table, and insert them into a dropdown list where the user can select one.

I have an error that I don't understand:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

Controller:

  public ActionResult Addnew()
        {
            var dba = new DefaultConnection();
            var query = dba.blob.Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.name_company,
                Selected = c.id.Equals(3)
            });
            var model = new Companylist
            {
                xpto = query.AsEnumerable()
            };

            return View(model);
        }

解决方案

You got this error because Entity Framework does not know how to execute .ToString() method in sql. So you should load the data by using ToList and then translate into SelectListItem as:

var query = dba.blob.ToList().Select(c => new SelectListItem
    {
    Value = c.id.ToString(),
    Text = c.name_company,
    Selected = c.id.Equals(3)
});

Edit: To make it more clear, Entity framework converts your use of query operators such as Select, Where ETC into an sql query to load the data. If you call a method like ToString(), which Entity Framework does not have an equivalent in sql, it will complain. SO the idea is to defer the use of such functions post data load. ToList, ToArray ETC force execution of the query thus loading of data. Once the data is loaded, any further operation (such as Select, Where ETC) is performed using Linq to Objects, on the data already in memory.

这篇关于LINQ到实体无​​法识别方法'System.String的ToString()“方法在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆