var menuQuery = repository.MenuMasters.OrderBy(c => c.Position).Select(c => new { ID = c.MenuMasterID, Position = (MenuItemPosition)c.Position + " - " + c.SitePage.Title });
ViewBag.ParentID = new SelectList(menuQuery, "ID", "Position", selectedParentId);
public int Position { get; set; }
public MenuItemPosition MenuPosition {
get { return (MenuItemPosition)Position; }
set { Position = (int)value; }
}
public enum MenuItemPosition {
Top = 1, Main = 2, Footer = 3
}
我收到一条错误,上面写着“无法将类型'类型名称'强制转换为类型'类型名称'.LINQ to Entities仅支持转换实体数据模型基元类型。”
MenuItemPosition是“Enum”,Title是“string”
我该如何解决这个问题?非常感谢〜!
答案 0 :(得分:1)
我认为你需要在OrderBy之后添加.ToList
,因为Linq to Entities不支持字符串连接..所以你的查询看起来像:
var menuQuery = repository.MenuMasters.OrderBy(c => c.Position).ToList().Select(c => new { ID = c.MenuMasterID, Position = ((MenuItemPosition)c.Position).convertToString() + " - " + c.SitePage.Title });
答案 1 :(得分:0)
尝试类似
的内容var menuQuery = repository.MenuMasters.OrderBy(c => c.Position).Select(c => new { ID = c.MenuMasterID, Position = c.Position.ToString() + " - " + c.SitePage.Title });