我正在尝试进行一个数据库调用来加载我的菜单,但在研究之后我注意到没有可用于自引用表的急切加载。现在它看起来像这样:
public ActionResult PopulateHeaderMenu()
{
using (var context = ww.WebObjs.WebDataContext.Get())
{
var menus = from menu in context.MenuCaches
where menu.URL == "MENU" && menu.ParentID == -1
select new MenuModel
{
ID = menu.ID,
Children = from x in menu.Children
select new MenuModel
{
ID = x.ID,
Children = from y in x.Children
select new MenuModel
{
ID = y.ID,
Children = from p in y.Children
select new MenuModel
{
ID = p.ID,
Children = from r in p.Children
select new MenuModel
{
ID =r.ID,
Children = from s in r.Children
select new MenuModel
{
ID =s.ID,
Children = from t in s.Children
select new MenuModel
{
ID = t.ID,
}
}
}
}
}
}
};
var model = menus.First();
return PartialView("_NaviPartial", model);
}
}
这工作得相当好,但它会生成 113 个单独的查询。我尝试使用 DataLoadWith(),但不能对自引用属性执行此操作。还有另一种方法可以做我在这里想做的事情吗?我采取了错误的方法吗?
谢谢