using System;
using System.Collections.Generic;
using System.Linq; namespace ConsoleAppTest
{
class Program
{
static void Main(string[] args)
{
var aa = new AA();
var tree = new Tree() { Id = , Name = "" };
aa.CreateTree(ref tree);
Console.WriteLine();
}
} public class Tree
{
public Tree()
{
Childrens = new List<Tree>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Tree> Childrens { get; set; } } public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int Fid { get; set; }
} public class AA
{
List<Product> Products = new List<Product>() {
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
new Product(){Id=,Name="",Fid=},
}; public void CreateTree(ref Tree tree)
{
var id = tree.Id;
var list = Products.Where(x => x.Fid == id).ToList();
if (list.Any())
{
foreach (var item in Products.Where(x => x.Fid == id).ToList())
{
var treeSon = new Tree
{
Id = item.Id,
Name = item.Name
};
tree.Childrens.Add(treeSon);
Console.WriteLine($"{tree.Id}");
CreateTree(ref treeSon);
}
}
else
{
Console.WriteLine($"{tree.Id}");
}
}
}
}
运行结果
.