title: How to manually mapping DTO WITHOUT using AutoMapper?
body: <p>I'm learning C#.NET Core and trying to create DTO mapping without using AutoMapper as I'm working on a small project alone and want to understand fundamental before using extra packages, surpringly I could not easily find answer at stackoverflow.com or I may use wrong keyword searching.</p>
  
  <p>BTW, below is my code which I successfully map to EmployeeForShortDto under GetEmployee method. Unfortunately, I don't how to map it under GetAllEmployee just because the return data is a collection, not a single record. Please advice.</p>
  
  <h3>EmployeeController.cs</h3>
  
  <pre><code>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&lt;IActionResult&gt; GetEmployee(int id)
          {
              var data = await _repo.GetEmployee(id);
              if (data == null) return NotFound();
              var dataDto = new EmployeeForShortDto()
              {
                  Id = data.Id,
                  EmpCode = data.EmpCode,
                  Fname = data.Fname,
                  Lname = data.Lname,
                  Age = NetCoreWebApplication1.Other.Extension.CalcAge(data.DateBirth)
              };
  
              return Ok(dataDto);
          }
  
          [HttpGet]
          public async Task&lt;IActionResult&gt; GetAllEmployee()
          {
              var data = await _repo.GetAllEmployee();
              return Ok(data);
          }
  
      }
  }
  </code></pre>
  
  <h3>MasterRepository.cs</h3>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.EntityFrameworkCore;
  using NetCoreWebApplication1.Models;
  
  namespace NetCoreWebApplication1.Repository
  {
      public class MasterRepository : IMasterRepository
      {
          private readonly PrDbContext _context;
  
          public MasterRepository(PrDbContext context)
          {
              _context = context;
          }
  
  
          // Employee
          public async Task&lt;List&lt;Employee&gt;&gt; GetAllEmployee()
          {
              var data = await _context.Employee.ToListAsync();
              return data;
          }
  
          public async Task&lt;Employee&gt; GetEmployee(int id)
          {
              var data = await _context.Employee.FirstOrDefaultAsync(x =&gt; x.Id == id);
              return data;
          }
  
          // Generic methods
          public void Add&lt;T&gt;(T entity) where T : class
          {
              _context.Add(entity);
          }
  
          public void Delete&lt;T&gt;(T entity) where T : class
          {
              _context.Remove(entity);
          }
  
          public async Task&lt;bool&gt; SaveAll()
          {
              return await _context.SaveChangesAsync() &gt; 0;
          }
      }
  }
  </code></pre>