繁体   English   中英

用C#自动映射

[英]Automapper with c#

我正在尝试通过自动映射器来提高我的api的性能。 我已经创建了目标DTO,并且已经有一个源mnodel。 我还创建了映射,但是当我映射此错误时。

 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.

请参阅下面的代码示例

  [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模型

    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; }
   }

多米安模型

    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; }
    }

映射资料

  Mapper.CreateMap<employeeinfo, employeeinfoDto>();

您可以像这样使用Queryable Extensions:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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