【发布时间】:2018-10-28 04:38:00
【问题描述】:
我有一个多层次的类结构。也就是说,一个“主题”可以有多个孩子,一个“主题”可以属于多个父级。这是我的班级结构:
public abstract class Hierarchy <T>{
public virtual ICollection<T> Parents { get; set; }
public virtual ICollection<T> Children{ get; set; }
}
public class Topic: Hierarchy <T>
{
public long ID {get;set;}
public string Title{get;set;}
}
现在对于每个主题以及 ID、标题,我想选择所有子 ID(也应该包括嵌套的子 ID)。
这是我的查询:
var result = from x in db.topics
select new TopicsDO{
Id = x.Id,
Title = x.Title,
ChildIds = x.Children.SelectMany(x=>x.Id) //This does not give nested child Ids, it just returns the immediate child Ids.
}
感谢您的帮助。
【问题讨论】:
标签: c# entity-framework linq