使用AutoMapper将ID列表转换为Object

时间:2018-06-23 06:46:27

标签: c# automapper

我有对象:

  [HttpPost]
    public ActionResult UploadFiles()
    {
        // Checking no of files injected in Request object  
        if (Request.Files.Count > 0)
        {
            try
            {
                //  Get all files from Request object  
                HttpFileCollectionBase files = Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    //string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";  
                    //string filename = Path.GetFileName(Request.Files[i].FileName);  

                    HttpPostedFileBase file = files[i];
                    string fname;

                    // Checking for Internet Explorer  
                    if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                    {
                        string[] testfiles = file.FileName.Split(new char[] { '\\' });
                        fname = testfiles[testfiles.Length - 1];
                    }
                    else
                    {
                        fname = file.FileName;
                    }

                    // Get the complete folder path and store the file inside it.  
                    fname = Path.Combine(Server.MapPath("~/Content/"), fname);
                    file.SaveAs(fname);
                }
                // Returns message that successfully uploaded  
                return Json("File Uploaded Successfully!");
            }
            catch (Exception ex)
            {
                return Json("Error occurred. Error details: " + ex.Message);
            }
        }
        else
        {
            return Json("No files selected.");
        }
    }

现在我有一个Dto了:

=max(0, arrayformula(SUM(if(L3:N3=L$1:N$1, P3, Q3))))
'if Georges score can be negative then,
=arrayformula(SUM(if(L3:N3=L$1:N$1, P3, Q3)))

用户可以在保存之前创建/发送GroupDto并将其转换为Group。

如何为此定义MappingProfile?

2 个答案:

答案 0 :(得分:2)

在映射配置文件中,只需使用MapFrom方法,并让AutoMapper知道如何获取如下数据:

CreateMap<Group, GroupDto>()
    .ForMember(
        destination => destination.Details, 
        options => options.MapFrom(
            source => source.Details.Select(detail => detail.Id).ToList()
        )
    );

侧面说明:请公开一个属性,而不要像您在示例中所做的那样显示字段。另外,请确保GroupDto类的属性也为public

答案 1 :(得分:1)

赞:

Mapper.CreateMap<Group, GroupDto>()
                .ForMember(d => d.Details,
                    opt =>
                        opt.MapFrom(
                            s => s.Details.Select(x=>x.Id).ToList()))