![](/img/trans.png)
[英]LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression
[英]LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method, and this method cannot be translated into a store expression."}
我在控制器中使用以下代碼
BlueBusDB_Context db = new BlueBusDB_Context();
List<SelectListItem> li =new List<SelectListItem>();
li = db.M_BLUEBUS_STATES.Select(s => new SelectListItem {Text = s.STATE_NAME, Value = Convert.ToString(s.STATE_CODE)}).ToList();
ViewBag.State = li;
return View();
然后在我的創建視圖中,我想綁定下拉列表,我使用的是波紋管
@Html.DropDownList("State",ViewBag.State as List<SelectListItem>)
我收到上述錯誤。
Linq to Entities不支持Convert
(請參閱支持的功能 )
我將首先創建一個匿名類型,然后將其轉換為SelectListItems以更好地分離關注點:
li = db.M_BLUEBUS_STATES
.Select(s => new { s.STATE_NAME, s.STATE_CODE })
.AsEnumerable()
.Select(x => new SelectListItem { Text = x.STATE_NAME, Value = x.STATE_CODE.ToString()})
.ToList();
您需要使用常規的ToString
方法,而不是Convert
類的方法:
li = db.M_BLUEBUS_STATES.Select(s => new SelectListItem {
Text = s.STATE_NAME,
Value = s.STATE_CODE.ToString()
}).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.