如何在自动映射器ProjectTo中处理空值

时间:2018-08-31 13:54:31

标签: entity-framework-core automapper

我正在使用带有实体框架核心2.1和自动映射器3.2的.NET Core应用程序。在下面的脚本中,尝试ProjectTo(myviewClass)时会得到空值并收到错误消息。现在,我知道可以通过do userRoleList.select(x=>x.ConsultationId) == null? null: projectTo在以下脚本中检查单个值,但问题是它的集合,因此如何应用类似的逻辑,就像您看到的那样,我正在获取consultation记录的集合ConsultationId包含在userRoleLits中。

LINQ

 (from user in Context.Users
  join userRole in Context.UserRoles on user.Id equals userRole.UserId into userRoleList
  where (user.Id == (UserId ?? user.Id))
  select new UsersWithAccessProfileDataView
     {
       UserId = user.Id,
       UserName = user.Name,
       Consultation = Context.Consultations.Where(x => userRoleList.Select(y => y.ConsultationId).Contains(x.Id)).ProjectTo<ConsultationDataView>() //need help here
     }).OrderBy(x => x.UserName);

模型视图类

public class UsersWithAccessProfileDataView
{
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public IQueryable<ConsultationDataView> Consultation { get; set; }
}

0 个答案:

没有答案