我有流程和任务这两个类。任务是一个相关实体,是可选的。我希望仅在不为null的情况下才能在select上映射Task属性。我该如何处理?
public class Process
{
public int Id {get;set;}
public string Description {get;set;}
public int? TaskId {get;set;}
public Task Task {get;set;}
}
public class Task
{
public int Id {get;set;}
public string Description {get;set;}
}
在我的剃须刀页面上
public PageViewModel Process {get;set;}
[BindProperty(SupportsGet = true)]
public int Id { get; set;}
public void OnGet()
{
Process = _context.Processes
.Select(p => new PageViewModel
{
Id = p.Id,
Description = p.Description,
HasTask = p.TaskId.HasValue,
TaskDescription = p.Task.Description // How to handle if task is null here?
})
.FirstOrDefault(p => p.Id == Id)
}
public class PageViewModel
{
public int Id{get;set;}
public string Description {get;set;}
public bool HasTask {get;set;}
public string TaskDescription {get;set;}
}
p.Task == null ? "" : p.Task.Description
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句