C#嵌套

using System;
using System.Collections.Generic;
using System.Linq;
 
public class Item
{
    public int Id { get; set; }
    public int? ParentId { get; set; }
}
 
public class Program
{
    public static void Main()
    {
        List<Item> items = new List<Item>
        {
            new Item { Id = 1, ParentId = null },
            new Item { Id = 2, ParentId = 1 },
            new Item { Id = 3, ParentId = 1 },
            new Item { Id = 4, ParentId = 2 },
            // 假设4是根项,但这里仍然包含在列表中
        };
 
        bool hasNestedItems = items.Any(i => IsNestedItem(items, i.Id));
 
        Console.WriteLine(hasNestedItems ? "存在嵌套调用" : "不存在嵌套调用");
    }
 
    private static bool IsNestedItem(List<Item> items, int id)
    {
        return items.Any(i => i.ParentId == id);
    }
}
posted @ 2024-10-15 16:15  陆陆无为而治者  阅读(1)  评论(0编辑  收藏  举报