简体   繁体   中英

Automapper with c#

I am trying to increase performance of my api with automapper. I have created the destination DTO and i have a source mnodel already. I've also created the mapping but when i map this error.

 The type arguments for method 'Enumerable.Select<TSource, TResult> 
 (IEnumerable<TSource>, Func<TSource, TResult>)' cannot be inferred from the 
 usage. Try specifying the type arguments explicitly.

See code sample below

  [Route("StaffInfo/{*job_title}")]
    public IHttpActionResult GetStaffInfoByTitle(string job_title)
    {

        var rStructure = _context.employeeinfo.Where(e => e.jobtitle.Contains(job_title)).ToList()
                         .Select(Mapper.Map<employeeinfo, employeeinfoDto>);




        if (rStructure == null)
        {
            return NotFound();
        }

        return Ok(rStructure);
    }

DTO model

    public class employeeinfoDto
   {
    public string employee_number { get; set; }
    public string name { get; set; }
    public string companyname { get; set; }
    public int employee_id { get; set; }
    public Byte? linemanager { get; set; }
   }

Domian model

    public class employeeinfo
    {  
     public string employee_number { get; set; }
     public string name { get; set; }
     public string companyname { get; set; }
     public int employee_id { get; set; }
     public Byte? linemanager { get; set; }
    }

Mapping profile

  Mapper.CreateMap<employeeinfo, employeeinfoDto>();

You can use the Queryable Extensions like so:

var rStructure = _context.employeeinfo.Where(e => e.jobtitle.Contains(job_title))
                     .ProjectTo<employeeinfoDto>().ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM