我想获得TaskHead组的任务列表,其中包含任务列表。
TaskItemToReturnHeadDTO。TaskHead == TaskItemToReturnDTO。ItemStage
实体public class TaskItem
{
[Key]
public int TaskItemId { get; set; }
public string Name { get; set; }
public string ItemStage { get; set; }
}
dto
public class TaskItemToReturnHeadDTO
{
public string TaskHead { get; set; }
public ICollection<TaskItemToReturnDTO> Tasks { get; set; }
}
public class TaskItemToReturnDTO
{
public int TaskItemId { get; set; }
public string Name { get; set; }
public string ItemStage { get; set; }
}
控制器
[HttpGet("all")]
public async Task<ActionResult<ICollection<TaskItem>>> GetTasks()
{
var tasks = await _context.TaskItems.ToListAsync();
var mappedTasks = _mapper.Map<IEnumerable<TaskItemToReturnHeadDTO>>(tasks);
return Ok(mappedTasks);
}
AutoMapper概要
CreateMap<TaskItem, TaskItemToReturnDTO>().ReverseMap();
CreateMap<TaskItemToReturnHeadDTO, TaskItem>().ReverseMap()
.ForMember(dto => dto.TaskHead, c => c.MapFrom(c => c.ItemStage))
.ForMember(dto => dto.Tasks, c => c.MapFrom(c => new List<TaskItemToReturnDTO> { new TaskItemToReturnDTO
{
TaskItemId = c.TaskItemId,
Name = c.Name,
ItemStage = c.ItemStage
}}));
这是我当前的结果,但是任务没有分组。
[
{
"taskHead": "Design done",
"tasks": [
{
"taskItemId": 1,
"name": "Issuing",
"itemStage": "Design done"
}
]
},
{
"taskHead": "Design done",
"tasks": [
{
"taskItemId": 2,
"name": "Issuing",
"itemStage": "Design done"
}
]
},
{
"taskHead": "Design being checked",
"tasks": [
{
"taskItemId": 4,
"name": "SF Joists",
"itemStage": "Design being checked"
}
]
},
{
"taskHead": "Design being checked",
"tasks": [
{
"taskItemId": 5,
"name": "FF Joists",
"itemStage": "Design being checked"
}
]
},
{
"taskHead": "Being issued",
"tasks": [
{
"taskItemId": 6,
"name": "Roof",
"itemStage": "Being issued"
}
]
}
]
下面我展示了它应该是什么样子。这是我的目标。
[
{
"taskHead": "Design done",
"tasks": [
{
"taskItemId": 1,
"name": "Issuing",
"itemStage": "Design done"
},
{
"taskItemId": 2,
"name": "Issuing",
"itemStage": "Design done"
}
]
},
{
"taskHead": "Design being checked",
"tasks": [
{
"taskItemId": 4,
"name": "SF Joists",
"itemStage": "Design being checked"
},
{
"taskItemId": 5,
"name": "FF Joists",
"itemStage": "Design being checked"
}
]
},
{
"taskHead": "Being issued",
"tasks": [
{
"taskItemId": 6,
"name": "Roof",
"itemStage": "Being issued"
}
]
}
]
我想我应该在某个地方使用GroupBy功能,但我不知道在哪里以及如何使用。也许我映射错了?
Automapper适用于TaskItemToReturnDTO
,但您不需要为TaskItemToReturnHeadDTO
创建映射器。相反,在映射之后使用linq进行分组,如下所示:
var mappedTasks = _mapper.Map<IEnumerable<TaskItemToReturnDTO>>(tasks);
var groupedTasks = mappedTasks
.GroupBy(task => task.ItemStage)
.Select(group => new TaskItemToReturnHeadDTO { TaskHead = group.Key, Tasks = group.ToList() });
@Oignon_Rouge谢谢,已经用非常相似的代码弄清楚了…你能检查一下我的版本是否正确吗?对我来说很好:)
[HttpGet("all")]
public async Task<ActionResult<ICollection<TaskItem>>> GetTasks()
{
var tasks = await _context.TaskItems.ToListAsync();
var mappedTasks = _mapper.Map<IEnumerable<TaskItemToReturnHeadDTO>>(tasks);
var groupedTasks = mappedTasks.GroupBy(z => z.TaskHead).Select(v => new TaskItemToReturnHeadDTO
{
TaskHead = v.Key,
Tasks = _mapper.Map<ICollection<TaskItemToReturnDTO>>(tasks).Where(x => x.ItemStage == v.Key).ToList()
});
return Ok(groupedTasks);
}