我在这个问题上卡了几天,希望能得到一些想法或帮助解决它。 我有一组对象。
public class Hierarchy
{
public Hierarchy(string iD, string name, int level, string parentID, string topParent)
{
ID = iD;
Name = name;
Level = level;
ParentID = parentID;
Children = new HashSet<Hierarchy>();
}
public string ID { get; set; }
public string Name{ get; set; }
public int Level { get; set; }
public string ParentID { get; set; }
public ICollection<Hierarchy> Children { get; set; }
}
我从实体中得到的 Linq 查询数据是:
ID Name Level ParentID
295152 name1 1 null
12345 child1 2 295152
54321 child2 2 295152
44444 child1a 3 12345
33333 child1b 3 12345
22222 child2a 3 54321
22221 child2b 3 54321
22002 child2c 3 54321
20001 child2a2 4 22222
20101 child2b2 4 22222
这些数据可能会延伸到未知的深度(我只展示了4个级别)。 最终,我将拥有一个层次结构对象,其中包含多个子对象,这些子对象又可以有多个子对象的集合......等等...... 始终只有一个顶级对象。
我尽可能在此项目中使用Linq。
显然,这需要某种递归方法,但我卡住了。任何想法或帮助将不胜感激。
TIA