在dto列表中设置默认值

时间:2019-03-13 11:49:50

标签: c# entity-framework-6

我有我的dto

例如:

public class CustomerDto
{
    public string Name {get;set;}
    public List<DepartmentDto> Departments {get;set;}
}

public class DepartmentDto 
{
    public string Name {get;set;}
}

我使用ef核心的选择查询:

    var data = db.Customers.Select(x => new CustomerDto
    {
        Name = x.Name,
        Departments = x.Departments.Select(e => new DepartmentDto
        {
            Name = e.Name
        })
    }).ToList();

执行查询后,如果我的部门列表为null,则需要创建一个默认值,例如:

没有部门以property名称记录

0 个答案:

没有答案