簡體   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