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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…