我有Linq查询,其中我对应答表应用左连接,以查看用户详细信息是否存在!如果有详细信息然后得到用户记录,如果用户记录没有设置空值,我正努力在这里设置空值。我使用autoMapper将dataModel转换为dataView。在linq查询中,请参阅'用户'属性,还附上了我在运动中获得的输出屏幕截图
var responsesList3 = (from response in Context.Responses
where response.ConsultationId == ConsultationId && response.ResponseTypeId == ResponseTypeId && response.ResponseCurrentStatus == ResponseStatusType
join respondent in Context.Respondents.Include(resp => resp.User) on response.RespondentId equals respondent.Id
//from respondentUser in Context.Users.Where(user => user.Id == respondent.UserId)
join respondentUser in Context.Users on respondent.UserId equals respondentUser.Id into rs
from respondentUserDetail in rs.DefaultIfEmpty()
join responseType in Context.ResponseTypes on response.ResponseTypeId equals responseType.Id
join dataEntryAnalyst in Context.Users on response.DataEntryAnalystId equals dataEntryAnalyst.Id
select new ResponseDataView
{
ResponseId = response.Id,
RespondentId = response.RespondentId,
Respondent = new RespondentDataView
{
RespondentId = respondent.Id,
UserId = respondent.UserId,
// User = respondent.UserId == null ? null : Context.Users.Where(user => user.Id == respondent.UserId).AsQueryable().ProjectTo<UserDataView>(),
// User = respondent.UserId == null ? : My.Mapper.Map<UserDataView>(respondent.User),
//(need to set User = null if Respondent.UserId ==null)???????????
User = My.Mapper.Map<UserDataView>(respondentUserDetail),
Name = respondent.Name,
Organisation = respondent.Organisation,
Address = respondent.Address,
Stakeholder = respondent.Stakeholder,
GenderType = respondent.GenderType
}
}
);
public struct RespondentDataView
{
public Guid RespondentId { get; set; }
public Guid? UserId { get; set; }
public UserDataView User { get; set; }
public string Name { get; set; }
public string Organisation { get; set; }
public string Address { get; set; }
public string Stakeholder { get; set; }
public GenderTypes GenderType { get; set; }
}
public struct UserDataView
{
public Guid UserId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
}
public UserProfile()
{
CreateMap<UserDataModel, UserDataView>()
.ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.Surname, opt => opt.MapFrom(src => src.Surname))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
;
}
答案 0 :(得分:0)
问题是由于struct作为我的dataView,解决方案将struct更改为Nullable,或者在我的情况下,我将其更改为class
public class UserDataView
{
public Guid UserId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
}
并在linq查询中,检查空值为
//remain code of linq continuous ......
Respondent = new RespondentDataView
{
RespondentId = respondent.Id,
UserId = respondent.UserId,
User = respondent.UserId == null ? null: My.Mapper.Map<UserDataView>(respondentUserDetail),
Name = respondent.Name,
Organisation = respondent.Organisation,
Address = respondent.Address,
Stakeholder = respondent.Stakeholder,
GenderType = respondent.GenderType
}