如何使用的ToString SelectListItem实体框架? [英] How to use ToString SelectListItem with Entity Framework?
问题描述
我有一些code如下图所示。我得到了运行时异常说道:
类型的异常的System.NotSupportedException发生在
System.Data.Entity.dll但在用户code结果没有处理
其他信息:LINQ实体无法识别方法'System.String的ToString()'方法,和这种方法不能
翻译成一家商店前pression。
块引用>由异常的原因
VALUE = sup.SupplierID.ToString()
和VALUE = cat.CategoryID.ToString()
这似乎实体框架不能产生与的ToString()
但SelectListItem
我必须给它一个字符串。谁能提供简单的修复方案将是巨大的AP preciate。
命名空间MvcApplication1.Models
{
公共类ProductEditViewModel:产品
{
公共IEnumerable的< SelectListItem> SupplierDropDownItems {搞定;组; }
公共IEnumerable的< SelectListItem> CategorieDropDownitems {搞定;组; }
}
}
[HTTPGET]
公众的ActionResult ProductEdit(的Int32产品编号)
{
VAR罗斯文=新NorthwindEntities(); 变种Q =从northwind.Products p
其中,p.ProductID ==产品编号
选择新ProductEditViewModel
{
的ProductID = p.ProductID,
产品名称= p.ProductName,
单价= p.UnitPrice,
SupplierDropDownItems =从燮在northwind.Suppliers选择新SelectListItem的{text = sup.CompanyName,值= sup.SupplierID.ToString(),选择= p.Supplier.SupplierID == p.SupplierID},
从猫CategorieDropDownitems =在northwind.Categories选择新SelectListItem的{text = cat.CategoryName,值= cat.CategoryID.ToString(),选择= p.Category.CategoryID == p.CategoryID},
停产= p.Discontinued
}; 返回查看(q.SingleOrDefault());
}
< DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.SupplierID,供应商ID,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.DropDownList(供应商ID,Model.SupplierDropDownItems,htmlAttributes:新{@class =表格控})
@ Html.ValidationMessageFor(型号=> model.SupplierID,新{@class =TEXT-危险})
< / DIV>
< / DIV>
解决方案使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.stringconvert(v=vs.110).aspx\"相对=nofollow> SqlFunctions.StringConvert 这样的:
值= SqlFunctions.StringConvert((双)cat.CategoryID)
另一种选择是枚举内存中的所有记录,然后使用
的ToString
这样的:从northwind.Suppliers.AsEnumerable SUP()
选择新SelectListItem的{text = sup.CompanyName,值= sup.SupplierID.ToString(),选择= p.Supplier.SupplierID == p.SupplierID},
AsEnumerable
会从数据库读取的所有记录,在内存中加载它们,所以你的电话的ToString
会不要求转换到SQL。I have some code like below. I got run time exception says:
"An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."The exception cause by
"Value = sup.SupplierID.ToString()"
and"Value = cat.CategoryID.ToString()"
It seems entity framework can not generate expression tree withToString()
but forSelectListItem
I have to give it a string.Who can provide simple fix solution will be great appreciate.
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>
解决方案Use SqlFunctions.StringConvert like:
Value = SqlFunctions.StringConvert((double)cat.CategoryID)
The other option is to enumerate all records in memory and then use
ToString
like:from sup in northwind.Suppliers.AsEnumerable() select new SelectListItem { Text = sup.CompanyName, Value = sup.SupplierID.ToString(), Selected = p.Supplier.SupplierID == p.SupplierID },
AsEnumerable
would fetch all the records from the database and load them up in memory, so your callToString
would not be required to translate to SQL.这篇关于如何使用的ToString SelectListItem实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!