我有一个抽象类,其中有 3 个从此类继承的其他类:-
public abstract class AbstractTask
{
[Key]
public int ID {get; set;}
[DisplayName("Featured Task")]
public bool Featured { get; set; }
// various other properties
}
public class HighLevelTask : AbstractTask
{
[DisplayName("Workstream Description")]
public String Workstream { get; set; }
public virtual ICollection<MidLevelTask> MidLevelTasks { get; set; }
}
public class MediumLevelTask : AbstractTask
{
public int HighLevelTaskID { get; set; }
public virtual ICollection<DetailLevelTask> DetailLevelTasks { get; set; }
}
public class DetailLevelTask : AbstractTask
{
public int MidLevelTaskID { get; set; }
}
因此,高级任务可以包含任意数量的中级任务,而中级任务可以包含任意数量的详细任务。任何级别的任务都可以通过从抽象类继承的属性设置为“精选”。
在 HTML 视图中,我想呈现一个嵌套的“特色”任务列表。所以我想在一个控制器动作中像这样来收集所有的特色任务,但我对呈现这个的最佳方式感到困惑。
var qryHighLevelTasks = from t in context.HighLevelTasks
where t.Featured == true
select t;
var rsHighLevelTasks = qryHighLevelTasks.ToList();
foreach(var highLevelTask in rsHighLevelTasks)
{
// get all mid level featured tasks related to this high level task
var qryMidTasks = from midLevelTasks in context.MidLevelTasks
where midLevelTasks.Featured == true
&& midLevelTasks.HighLevelTaskID == highLevelTask.ID
select midLevelTasks;
var rsMidLevelTasks = qryMidTasks.ToList();
foreach (var midLevelTask in rsMidLevelTasks)
{
// get all detail level featured tasks related to this mid level task
var qryDetailTasks = from detailLevelTasks in context.DetailLevelTasks
where detailLevelTasks.Featured == true
&& detailLevelTasks.MidLevelTaskID == midLevelTask.ID
select detailLevelTasks;
var rsDetailLevelTasks = qryDetailTasks.ToList();
}
}
也许我应该有一个复合模型来表示特色任务?或者,还有更好的方法?有什么建议吗?