2

我正在尝试使用Linq 和 entity制作动态菜单。我想将查询结果直接添加到数据表中。选择查询(如下图)抛出异常。例外:

LINQ to Entities 无法识别方法 'System.Data.DataRow LoadDataRow(System.Object[], Boolean)' 方法,并且此方法无法转换为存储表达式

有替代方案吗?下面是我的代码。

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));
4

1 回答 1

2

您不能调用无法翻译成的方法SQL query,您应该分两步进行,如下所示:

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);
于 2013-09-12T03:30:01.900 回答