0

LINQ to Entities does not recognize the method 'System.String ToString()' method.

List <MenuModels> _menus = 
    _entity.SubMenus
        .Where(x => x.RoleId.Split(',').ToString()  == _loginCredentials.UserRoleId)
        .Select(x => new MenuModels
        {
            MainMenuId = x.MainMenu.Id,
            MainMenuName = x.MainMenu.MainMenu1,
            SubMenuId = x.Id,
            SubMenuName = x.SubMenu1,
            ControllerName = x.Controller,
            ActionName = x.Action,
            RoleId = x.RoleId,
            RoleName=x.Role.Roles
        }).ToList(); 

https://i.stack.imgur.com/4gt8n.png

xdtTransform
  • 1,986
  • 14
  • 34
  • 3
    x.RoleId.Split(',') will not produce a String but a String Array. What are you trying to do? – Tyress May 16 '19 at 08:12
  • 1
    Entity Framework is trying to convert this query to SQL but it doesn't know what do do with the `ToString` method. It will likely also fail on the `Split` too. So you need to do this all in memory, or I would strongly suggest you fix your database model. If you need to "split" a value then you should have those items in a separate table. – DavidG May 16 '19 at 08:14
  • Possible duplicate of [LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression](https://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method) – Wai Ha Lee May 16 '19 at 08:59

1 Answers1

1

Maybe you want to get all the submenus that the RoleId contains the UserRoleId. In this case you must to use:

List<MenuModels> _menus = _entity.SubMenus.Where(x => x.RoleId.Contains(_loginCredentials.UserRoleId)).Select ...
Roberto
  • 193
  • 1
  • 11