首页 > 解决方案 > 如何使用 where 语句在两个表之间创建 Map?

问题描述

我有医生和预约表。在我的 DTO 上,我有 DoctorName 字段,我想通过 AutoMapper 从医生表中获取该名称。我尝试在 AutoMapperProfile 上进行如下映射,但它说cannot convert from 'bool' to 'System.Func<<char,bool>。这两个值都是我想要映射的字符串,所以没有意义。下面是我的模型、DTO 和 CreateMap。

医生

public class Doctor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public Department Department { get; set; }
        public int DepartmentId { get; set; }
    }

预约

   public class Appointment
    {
        public int Id { get; set; }
        public DateTime AppointmentDate { get; set; }
        public int DoctorId { get; set; }
        public Doctor Doctor { get; set; }
        public int? PatientId { get; set; }
        public Patient Patient { get; set; }
    }

AutoMapperProfile

      CreateMap<Appointment, AvilableAppointmentDto>()
                    .ForMember(dep => dep.DoctorName, 
opt => opt.MapFrom(src => 
src.Doctor.Name.Where(src.DoctorId==src.Doctor.Id)));      

DTO

   public class AvilableAppointmentDto
{
    public int Id { get; set; }
    public DateTime AppointmentDate { get; set; }
    public int DoctorId { get; set; }        
    public string DoctorName { get; set; }

}

UPDATE(Solution) Dto 应该映射为 IENumerable。如下所示

    var returnAppointments = _mapper.Map<IEnumerable<AvilableAppointmentDto>>(availableAppointments);

标签: entity-frameworksqliteasp.net-core

解决方案


下面的东西对我有用

映射:

CreateMap<Appointment, AvilableAppointmentDto>(MemberList.Destination)
                    .ForMember(dep => dep.DoctorName, opt => opt.MapFrom(src => src.Doctor.Name));

实体类:

public class Doctor
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public int DepartmentId { get; set; }
}

public class Appointment
{
    public int Id { get; set; }
    public DateTime AppointmentDate { get; set; }
    public int DoctorId { get; set; }
    public Doctor Doctor { get; set; }
    public int? PatientId { get; set; }
}

public class AvilableAppointmentDto
{
    public int Id { get; set; }
    public DateTime AppointmentDate { get; set; }
    public int DoctorId { get; set; }
    public string DoctorName { get; set; }

}

控制器:

    [HttpGet]
    public ActionResult Get([FromServices] IMapper mapper)
    {
        Appointment ap = new Appointment()
        {
            AppointmentDate = DateTime.Now,
            DoctorId = 1,
            Id = 2,
            PatientId = 3,
            Doctor = new Doctor()
            {
                Id = 1,
                DepartmentId = 2,
                Name = "Ajay",
                Title = "Mr"
            }
        };

        var x = mapper.Map<AvilableAppointmentDto>(ap);

        return Ok(x);
    }

启动.cs

    public void ConfigureServices(IServiceCollection services)
    {            
        var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile<MappingProfile>();
        });

        var mapper = mappingConfig.CreateMapper();

        services.AddSingleton(mapper);
    }

包装参考。

<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.0.0" />

推荐阅读