以下是查询:
using (var _context = GetCommonDataContext())
{
return await _context.AnswerSelectListEntries
.Include(asle => asle.Optional)
.OrderBy(asle => asle.DisplayOrder)
.Select(asle => new AnswerSelectListEntryDTO()
{
Id = asle.Id,
AnswerSelectListId = asle.AnswerSelectListId,
ShortAnswer = asle.ShortAnswer,
LongAnswer = asle.LongAnswer,
CounterpartyGroupId = asle.CounterpartyGroupId,
Optional = new AnswerDefDTO()
{
Id = asle.Optional.Id,
Name = asle.Optional.Name,
AnswerType = asle.Optional.AnswerType,
MultiplierAnswerType = asle.Optional.MultiplierAnswerType,
IntRangeLow = asle.Optional.IntRangeLow,
IntRangeHigh = asle.Optional.IntRangeHigh,
AnswerSelectListId = asle.Optional.AnswerSelectListId
},
AnswerDefId = asle.AnswerDefId,
PartyType = asle.PartyType,
DisplayOrder = asle.DisplayOrder
}).ToListAsync();
}
我收到一个错误(从子查询抛出),Id和AnswerType(它是一个枚举)无法转换为对应的(int和enum),因为它们是null。问题是它们是不可为空的(在db中不是null)。不知何故,asle.Optional的属性被设置为null(即使它们是不可为空的)。我猜这与异步有关,但不确定是什么。
修正使用:
Optional = asle.Optional == null ? null : new AnswerDefDTO()...
答案 0 :(得分:1)
我的猜测是,因为那个东西是可选的,并且正在进行某种类型的连接,很可能是数据库中的左连接,这些列作为DBNull回来只是因为它们实际上并不是数据库。我建议你运行一个SELECT * FROM Optional,其中ID =无论是asle.Id是什么。