entity-framework - 如何使用 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);
解决方案
下面的东西对我有用
映射:
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" />
推荐阅读
- c# - Unity AR 相机可以像 VR 一样渲染 - 混合现实
- javascript - 如何防止对特定 div 的 onClick
- apache-kafka - 使用 KSQL 将表从 Kafka 转储到 MariaDB
- android - 是否有任何用于 React 的动态日历调度程序 - 用于 react native 的 react-big-scheduler?
- ios - Swift:计算的图像文件的 md5 哈希与终端和其他哈希生成器不匹配
- flutter - Flutter 用变量过滤数据
- html - 在我的投资组合/简历网站上工作,但菜单没有像我想要的那样悬停
- amazon-ec2 - 无法使用 Terraform 创建 EC2。路由表关联卡在创建模式
- mysql - 如何合并 2 个相似的查询?
- html - 我的背景图片没有出现在 Github 上