Reputation: 6656
I am trying to make dynamic menu using Linq and entity. I wants to add query result directly into datatable. select query (as shown below) throws an exception. Exception:
LINQ to Entities does not recognize the method 'System.Data.DataRow LoadDataRow(System.Object[], Boolean)' method, and this method cannot be translated into a store expression
Is there alternative? Here below is my code.
DataTable dtMenu = new DataTable();
dtMenu.Columns.Add("MenuName", typeof(string));
dtMenu.Columns.Add("SubMenuName", typeof(string));
dtMenu.Columns.Add("FileName", typeof(string));
var menuItems = (from vUser in dbMenu.VirtualUserMenuMaps
join menu in dbMenu.MenuMasters on vUser.MenuId equals menu.MenuId
join subMenu in dbMenu.SubMenuMasters on vUser.SubMenuId equals subMenu.SubMenuId
where vUser.VirtualId == virtualId
orderby menu.MenuId, subMenu.DisplayOrder
select dtMenu.LoadDataRow(
new object[] { menu.MenuName, subMenu.SubMenuName, subMenu.FileName }, false));
Upvotes: 2
Views: 1571
Reputation: 63327
You can't call a method that can't be translated into SQL query
, you should do in 2 steps like this:
var menuItems = from vUser in dbMenu.VirtualUserMenuMaps
join menu in dbMenu.MenuMasters on vUser.MenuId equals menu.MenuId
join subMenu in dbMenu.SubMenuMasters on vUser.SubMenuId equals subMenu.SubMenuId
where vUser.VirtualId == virtualId
orderby menu.MenuId, subMenu.DisplayOrder
select new { menu.MenuName, subMenu.SubMenuName, subMenu.FileName };
foreach(var row in menuItems)
dtMenu.LoadDataRow(new object[]{row.MenuName, row.SubMenuName, row.FileName}, false);
Upvotes: 2