我有这两个查询,其中第一个返回组织与部门,第二个返回没有部门的组织,之后我使用Union()
作为我的最终查询,因为我想在表中显示两个结果。
Union()
要求在两个查询中完全使用相同的属性,因此我必须在第二个查询中分配“空”Department
对象,但我需要将Department Name
设为空它引发了我null exception
,我不知道它为什么要抛出它,它对我来说没有意义。
当我尝试将organization.Name
分配到Department.Name
时突然发现它根本没有扔给我null exception
。
是否有可能将空字符串分配到Department name
?
var query1 = UserAccountOwnSuppliers
.Where(organization => organization.IsBuyer)
.SelectMany(organization => organization.Departments
.Select(department =>
new UserAccountMarketsForSupplierDTO
{
Organization = new OrganizationBaseDTO
{
Id = organization.Id,
Name = organization.Name,
RegionId = organization.RegionId,
CreatorDepartmentId = organization.CreatorDepartmentId,
CreatorOrganizationId = organization.CreatorOrganizationId,
CreatorId = organization.CreatorId,
},
Department = new DepartmentBaseDTO
{
Id = department.Id,
Name = department.Name
}
})).OrderBy(dto => dto.Organization.Name);
var query2 = UserAccountOwnSuppliers
.Where(organization => organization.IsBuyer)
.Select(organization => new UserAccountMarketsForSupplierDTO
{
Organization = new OrganizationBaseDTO
{
Id = organization.Id,
Name = organization.Name,
RegionId = organization.RegionId,
CreatorDepartmentId = organization.CreatorDepartmentId,
CreatorOrganizationId = organization.CreatorOrganizationId,
CreatorId = organization.CreatorId,
},
Department = new DepartmentBaseDTO
{
Id = 0,
//here it throws null exception when I try to assign some value
Name = "---"
}
}).OrderBy(dto => dto.Organization.Name); ;
var query = query2.Union(query1);
return query.OrderBy(dto => dto.Organization.Name);
DTO的
public class DepartmentBaseDTO : IEntity<int>
{
public string Name { get; set; }
public int Id { get; set; }
}
public class OrganizationBaseDTO : IEntity<int>
{
public string Name { get; set; }
public int? ParentOrganizationId { get; set; }
public int? RegionId { get; set; }
public int CreatorId { get; set; }
public int CreatorOrganizationId { get; set; }
public int CreatorDepartmentId { get; set; }
public string Code { get; set; }
}
public class UserAccountMarketsForSupplierDTO : IEntity<int>
{
public OrganizationBaseDTO Organization { get; set; } = new OrganizationBaseDTO();
public DepartmentBaseDTO Department { get; set; } = new DepartmentBaseDTO();
}