0
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
}

I got an error which said "Unable to cast the type 'Type Name' to type 'Type Name'. LINQ to Entities only supports casting Entity Data Model primitive types."

MenuItemPosition is "Enum", Title is "string"

How can I fix this? Many thanks~!

Phil
  • 42,255
  • 9
  • 100
  • 100
Ricky Yip
  • 303
  • 1
  • 4
  • 11

2 Answers2

1

I think you need to add .ToList after OrderBy, as string concatenation is not supported with Linq to Entities.. So your query will look something like :

var menuQuery = repository.MenuMasters.OrderBy(c => c.Position).ToList().Select(c => new { ID = c.MenuMasterID, Position = ((MenuItemPosition)c.Position).convertToString() + " - " + c.SitePage.Title });
Dhaval Shukla
  • 1,127
  • 2
  • 10
  • 19
0

Try something like

var menuQuery = repository.MenuMasters.OrderBy(c => c.Position).Select(c => new { ID = c.MenuMasterID, Position = c.Position.ToString() + " - " + c.SitePage.Title });
Jayanga
  • 889
  • 6
  • 15