我正在使用.NET Core 2.2和EF Core编写API,该项目包含一个敏捷项目经理。我正在尝试从数据库实体映射到dto。
问题是,例如,要计算某项任务的预算指标,我需要知道其子任务的预算指标。
config.CreateMap<Task, TaskWithIndicators>()
.ForMember(dest => dest.Indicators.Completion,
opt => opt.MapFrom(src => src.SubTasks.Sum(st => st.Progress)))
.ForMember(dest => dest.Indicators.UsedBudget,
opt => opt.MapFrom(src => src.SubTasks.Sum(st => st.InvestedCash)))
.ForMember(dest => dest.Indicators.RemainingDays,
opt => opt.MapFrom(src => src.SubTasks.Sum(st => st
.EstimatedStartDate.AddDays(st.EstimatedDuration)
.Subtract(DateTimeOffset.Now)
.TotalDays)));
因此,对于活动我必须做同样的事情:
config.CreateMap<Activity, ActivityWithIndicators>()
.ForMember(dest => dest.Indicators.Completion,
opt => opt.MapFrom(src =>
src.Tasks.Sum(t => t.SubTasks.Sum(st =>
st.Progress))))
.ForMember(dest => dest.Indicators.UsedBudget,
opt => opt.MapFrom(src =>
src.Tasks.Sum(t => t.SubTasks.Sum(st => st.InvestedCash))))
.ForMember(dest => dest.Indicators.RemainingDays,
opt => opt.MapFrom(src =>
src.Tasks.Sum(t =>
t.SubTasks.Sum(st =>
st.EstimatedStartDate
.AddDays(t.EstimatedDuration)
.Subtract(DateTimeOffset.Now)
.TotalDays))));
然后为了交付,我有这个:
config.CreateMap<Deliverable, DeliverableWithIndicators>()
.ForMember(dest => dest.Indicators.Completion,
opt => opt.MapFrom(src =>
src.Activities.Sum(a => a.Tasks.Sum(t =>
t.SubTasks.Sum(st =>
st.Progress)))))
.ForMember(dest => dest.Indicators.UsedBudget,
opt => opt.MapFrom(src =>
src.Activities.Sum(a =>
a.Tasks.Sum(t =>
t.SubTasks.Sum(st =>
st.InvestedCash)))))
.ForMember(dest => dest.Indicators.RemainingDays,
opt => opt.MapFrom(src =>
src.Activities.Sum(a =>
a.Tasks.Sum(t =>
t.SubTasks.Sum(st =>
st.EstimatedStartDate
.AddDays(t.EstimatedDuration)
.Subtract(DateTimeOffset.Now)
.TotalDays)))));
那么,这是添加嵌套求和方法的最佳方法吗?
这是层次结构:
谢谢