2016-12-07 81 views
0

我已經創建了下面在C#訂購模型的列表

[ 
    { 
     "MenuId": 1, 
     "ParentMenuId": 0 
    }, 
    { 
     "MenuId": 2, 
     "ParentMenuId": 1 
    }, 
    { 
     "MenuId": 3, 
     "ParentMenuId": 0 
    }, 
    { 
     "MenuId": 4, 
     "ParentMenuId": 3 
    }, 
    { 
     "MenuId": 5, 
     "ParentMenuId": 4 
    }, 
    { 
     "MenuId": 6, 
     "ParentMenuId": 3 
    }, 
    { 
     "MenuId": 7, 
     "ParentMenuId": 1 
    }, 
    { 
     "MenuId": 8, 
     "ParentMenuId": 4 
    } 
] 

的問題是,我需要所有的孩子穿越另一名家長之前來父母在爲給它返回一個JSON一個簡單的webapi2程序。 MenuId 6,7 & 8應分別小於8,2 & 5。也就是說,我需要這個JSON的順序是完全一樣

[ 
    { 
     "MenuId": 1, 
     "ParentMenuId": 0 
    }, 
    { 
     "MenuId": 2, 
     "ParentMenuId": 1 
    }, 
    { 
     "MenuId": 7, 
     "ParentMenuId": 1 
    }, 
    { 
     "MenuId": 3, 
     "ParentMenuId": 0 
    }, 
    { 
     "MenuId": 4, 
     "ParentMenuId": 3 
    }, 
    { 
     "MenuId": 5, 
     "ParentMenuId": 4 
    }, 
    { 
     "MenuId": 8, 
     "ParentMenuId": 4 
    }, 
    { 
     "MenuId": 6, 
     "ParentMenuId": 3 
    } 
] 

要做到這一點,我已經編寫以下用C#中的意大利麪條的代碼,它的工作原理,給我的結果,但我需要知道有沒有其他更好的方法。任何意見將是有益的。謝謝。

var rolerights = new List<RoleRightsModel>(); 
var rights = _rightsRepository.GetRights(RoleId); 
foreach (var right in rights) 
{ 
    if (right.ParentMenuId == 0) 
    { 
     rolerights.Add(right); 
     var rgs1 = rights.Where(p => p.ParentMenuId == right.MenuId).ToList(); 
     foreach (var rg1 in rgs1) 
     { 
      rolerights.Add(rg1); 
      var rgs2 = rights.Where(p => p.ParentMenuId == rg1.MenuId).ToList(); 
      foreach (var rg2 in rgs2) 
      { 
       rolerights.Add(rg2); 
       var rgs3 = rights.Where(p => p.ParentMenuId == rg2.MenuId).ToList(); 
       foreach (var rg3 in rgs3) 
       { 
        rolerights.Add(rg3); 
       } 
      } 
     } 
    } 
} 
//This code works only upto two levels of nesting, if i need three levels, i need to add another loop. 
+0

[排序使用lambda/LINQ到對象的列表(HTTP的可能重複:// stackoverflow.com/questions/722868/sorting-a-list-using-lambda-linq-to-objects) – Liam

+2

簡單,修復生成json對象數組的代碼? – grmbl

+1

或者使用Json.Net創建一個集合,排序並讓Json.Net再次解析爲json。 – grmbl

回答

1

遞歸將有助於:

using Newtonsoft.Json; /* need to Newtonsoft.Json - NuGet Package */ 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 


namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string menudata = @"[ 
     { 
      ""MenuId"": 1, 
      ""ParentMenuId"": 0 
     }, 
     { 
      ""MenuId"": 2, 
      ""ParentMenuId"": 1 
     }, 
     { 
      ""MenuId"": 3, 
      ""ParentMenuId"": 0 
     }, 
     { 
      ""MenuId"": 4, 
      ""ParentMenuId"": 3 
     }, 
     { 
      ""MenuId"": 5, 
      ""ParentMenuId"": 4 
     }, 
     { 
      ""MenuId"": 6, 
      ""ParentMenuId"": 3 
     }, 
     { 
      ""MenuId"": 7, 
      ""ParentMenuId"": 1 
     }, 
     { 
      ""MenuId"": 8, 
      ""ParentMenuId"": 4 
     } 
    ]"; 

      Console.WriteLine(menudata); 

      var r = JsonConvert.DeserializeObject<List<Menu>>(menudata); 
      r = RecursiveTreeSort(r); 
      Console.WriteLine(JsonConvert.SerializeObject(r, Formatting.Indented)); 
      Console.ReadLine(); 

     } 

     public static List<Menu> RecursiveTreeSort(List<Menu> source, int parentMenuId = 0) 
     { 
      List<Menu> result = new List<Menu>(); 
      foreach (var item in source.Where(s => s.ParentMenuId == parentMenuId)) 
      { 
       result.Add(item); 
       result.AddRange(RecursiveTreeSort(source, item.MenuId)); 
      } 
      return result; 
     } 
    } 


    public class Menu 
    { 
     public Menu() { } 
     public int MenuId { get; set; } 
     public int ParentMenuId { get; set; } 
    } 

} 
+0

謝謝@HGMamaci,它的工作很棒,非常完美。再次感謝。 –

+0

我的榮幸。 :-) – HGMamaci

0

使用Json.Net(NuGet包):

class Program 
    { 
     static void Main(string[] args) 
     { 
      var json = @"[ 
    { 
     ""MenuId"": 1, 
     ""ParentMenuId"": 0 
    }, 
    { 
     ""MenuId"": 2, 
     ""ParentMenuId"": 1 
    }, 
    { 
     ""MenuId"": 3, 
     ""ParentMenuId"": 0 
    }, 
    { 
     ""MenuId"": 4, 
     ""ParentMenuId"": 3 
    }, 
    { 
     ""MenuId"": 5, 
     ""ParentMenuId"": 4 
    }, 
    { 
     ""MenuId"": 6, 
     ""ParentMenuId"": 3 
    }, 
    { 
     ""MenuId"": 7, 
     ""ParentMenuId"": 1 
    }, 
    { 
     ""MenuId"": 8, 
     ""ParentMenuId"": 4 
    } 
]"; 

      var collection = JsonConvert.DeserializeObject<IEnumerable<dynamic>>(json); 

      // apply sorting here 
      var sorted = collection.OrderBy(i => i.ParentMenuId); 

      var sortedJson = JsonConvert.SerializeObject(sorted); 

     } 
    } 
+2

如果你想要編譯器安全的代碼,你應該創建一個保存你的json對象屬性而不是動態的對象。 – grmbl