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);
}
}
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);
}
}
有志者,事竟成,破釜沉舟,百二秦关终属楚; 苦心人,天不负,卧薪尝胆,三千越甲可吞吴。