How to use ToString SelectListItem with Entity Framework?

I have a code as shown below. I got a runtime exception:

A "System.NotSupportedException" type exception occurred in System.Data.Entity.dll, but was not processed in the user code
Additional information: LINQ to Entities does not recognize the "System.String ToString ()" method, and this method cannot be translated into a storage expression. "

The exception reason for "Value = sup.SupplierID.ToString()" and "Value = cat.CategoryID.ToString()" It seems the framework entity cannot generate an expression tree using ToString() , but for SelectListItem I need to give it a string.

Who can provide a simple solution will be very valuable.


 namespace MvcApplication1.Models { public class ProductEditViewModel : Product { public IEnumerable<SelectListItem> SupplierDropDownItems { get; set; } public IEnumerable<SelectListItem> CategorieDropDownitems { get; set; } } } [HttpGet] public ActionResult ProductEdit(Int32 ProductId) { var northwind = new NorthwindEntities(); var q = from p in northwind.Products where p.ProductID == ProductId select new ProductEditViewModel { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice, SupplierDropDownItems = from sup in northwind.Suppliers select new SelectListItem { Text = sup.CompanyName, Value = sup.SupplierID.ToString(), Selected = p.Supplier.SupplierID == p.SupplierID }, CategorieDropDownitems = from cat in northwind.Categories select new SelectListItem { Text = cat.CategoryName, Value = cat.CategoryID.ToString(), Selected = p.Category.CategoryID == p.CategoryID }, Discontinued = p.Discontinued }; return View(q.SingleOrDefault()); } <div class="form-group"> @Html.LabelFor(model => model.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("SupplierID", Model.SupplierDropDownItems, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.SupplierID, "", new { @class = "text-danger" }) </div> </div> 
+1
source share
3 answers

Use SqlFunctions.StringConvert , for example:

 Value = SqlFunctions.StringConvert((double)cat.CategoryID) 

Another option is to list all the entries in memory, and then use ToString as:

 from sup in northwind.Suppliers.AsEnumerable() select new SelectListItem { Text = sup.CompanyName, Value = sup.SupplierID.ToString(), Selected = p.Supplier.SupplierID == p.SupplierID }, 

AsEnumerable will retrieve all records from the database and load them into memory, so your ToString call will not need to be translated into SQL.

+1
source

Instead of .ToString (), the user is Convert.ToString (your value);

+1
source

It seems that you are using an old version of EF - ToString() , supported by newer versions of Entity Framework (since version 6.1)

See Release Notes for EF 6.1:

What's in EF6.1

EF6.1 adds the following new features:
[...]

  • Support .ToString, String.Concat and HasFlags enumeration in LINQ queries.
+1
source

All Articles