Entity framework core 如何在Auto mapper ProjectTo中处理空值
我正在使用Entity Framework Core 2.1和auto mapper 3.2开发.NET Core应用程序。在下面的脚本中,当我尝试Entity framework core 如何在Auto mapper ProjectTo中处理空值,entity-framework-core,automapper,Entity Framework Core,Automapper,我正在使用Entity Framework Core 2.1和auto mapper 3.2开发.NET Core应用程序。在下面的脚本中,当我尝试ProjectTo(myviewClass)时,我得到了null值,并得到了一个错误。现在我知道我可以通过douserRoleList检查下面脚本中的单个值?null:projectTo但问题是,它的集合,所以我如何应用类似于您所看到的逻辑,我将获得一个咨询记录集合,其中它的咨询ID包含在用户角色列表中 林克 (来自上下文中的用户。用户 将user.
ProjectTo(myviewClass)
时,我得到了null值,并得到了一个错误。现在我知道我可以通过douserRoleList检查下面脚本中的单个值?null:projectTo
但问题是,它的集合,所以我如何应用类似于您所看到的逻辑,我将获得一个咨询记录集合,其中它的咨询ID
包含在用户角色列表中
林克
(来自上下文中的用户。用户
将user.Id等于userRole.UserId的Context.UserRoles中的userRole加入userRoleList
其中(user.Id==(UserId??user.Id))
选择新用户SwitAccessProfileDataView
{
UserId=user.Id,
用户名=user.Name,
Consultation=Context.Consultations.Where(x=>userRoleList.Select(y=>y.ConsultationId).Contains(x.Id)).ProjectTo()//此处需要帮助
}).OrderBy(x=>x.UserName);
模型视图类
public类用户swithaccessprofiledataview
{
公共Guid用户标识{get;set;}
公共字符串用户名{get;set;}
公共可查询咨询{get;set;}
}
您可以尝试其中(x=>userRoleList.Select(y=>y.ConsultationId).Contains(x.Id)).SingleOrDefault().ProjectTo()
?
(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; }
}