在C#中订购模型列表

时间:2016-12-07 11:54:19

标签: c# json linq

我创建了一个简单的webapi2程序,它返回一个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
    }
]

问题是,我需要所有的孩子在经过另一个父母之前来到父母之下。 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.

2 个答案:

答案 0 :(得分: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; }
    }

}

答案 1 :(得分: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);

        }
    }