0

我有具有以下特征的菜单表:

tblMenu: ID , Title , ParentID , OrderM

我想使用 ParentID 和 OrderM 选择菜单和订单,如下所示:

前任:

表中数据:

ID = 1  Title = menu1 ParentID = 0 OrderM = 1
ID = 2  Title = menu2 ParentID = 0 OrderM = 2
ID = 3  Title = menu3 ParentID = 0 OrderM = 3
ID = 4  Title = submenu2-1 ParentID = 2 OrderM = 1
ID = 5  Title = submenu2-2 ParentID = 2 OrderM = 2
ID = 6  Title = submenu1-2 ParentID = 1 OrderM = 2
ID = 7  Title = submenu1-1 ParentID = 1 OrderM = 1

我想要选择 LINQ 的以下结果:

menu1
submenu1-1
submenu1-2
menu2
submenu2-1
submenu2-2
menu3
4

2 回答 2

2

这应该有效(示例):

var result =
  l.Where(c => c.ParentID == 0)
   .Select(c => new {Menu = c, Sub = l.Where(ci => ci.ParentID == c.ID).OrderBy(s => s.OrderM)})
   .OrderBy(ao => ao.Menu.OrderM)
   .SelectMany(ao => ao.Sub.Count() == 0 ? new List<C> {ao.Menu} : new List<C> {ao.Menu}.Concat(ao.Sub));
于 2013-03-16T19:07:44.653 回答
0

如果是您遇到问题的顺序,请尝试使用 OrderBy 和 ThenBy 来实现您需要的顺序。

例如:

 var items = source.OrderBy(i => i.OrderM).ThenBy(o => o.ParentID);
于 2013-03-16T18:49:17.540 回答