我正在学习 C#.NET Core 并尝试在不使用 AutoMapper 的情况下创建 DTO 映射,因为我正在单独处理一个小项目,并且想在使用额外的包之前了解基础知识,令人惊讶的是,我无法在 stackoverflow.com 或我上轻松找到答案可能使用错误的关键字搜索。
顺便说一句,下面是我在 GetEmployee 方法下成功映射到 EmployeeForShortDto 的代码。不幸的是,我不知道如何在 GetAllEmployee 下映射它,因为返回数据是一个集合,而不是单个记录。请指教。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetCoreWebApplication1.Dto;
using NetCoreWebApplication1.Repository;
using NetCoreWebApplication1.Other;
namespace NetCoreWebApplication1.Controller
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private readonly IMasterRepository _repo;
public EmployeeController(IMasterRepository repo)
{
_repo = repo;
}
[HttpGet("{id}")]
public async Task<IActionResult> GetEmployee(int id)
{
var data = await _repo.GetEmployee(id);
if (data == null) return NotFound();
var dataDto = …Run Code Online (Sandbox Code Playgroud)