我有以下POCO:
public class User {
public ICollection<DepartmentPosition> DepartmentPositions { get; set; }
public PerformanceRecord PerformanceRecord { get; set; }
}
其中
DepartmentPosition
定义为:public class DepartmentPosition {
public Department Department { get; set; }
public PositionType PositionType { get; set; }
}
PositionType
是定义为: public enum PositionType : byte {
Employee = 0,
Manager = 1
}
我希望能够查询
Manager
是否能够看到Employee
PeformanceRecord
。此标准是:
如果经理的
DepartmentPosition
的PositionType
为Manager
,并且该特定DepartmentPosition
的Department
等于雇员的Department
的DepartmentPositions
,则经理为能够查看员工的绩效记录。为此使用了一个规范类:
public CanUserSeePerformanceRecord() {
public bool IsSatisfiedBy(User fooUser, User barUser) {
// PSUEDO CODE
// Returns true if:
// fooUser and barUser both have a DepartmentPosition with the same Department AND for barUser, the PositionType of the DepartmentPosition is Manager
}
}
我认为您可以使用Linq相交或类似方法来做到这一点,但不确定如何包含
barUser
必须持有标记为DepartmentPosition
的Manager
的条件。 最佳答案
假设部门有一个ID字段来唯一标识它。您应该可以执行以下操作:
return barUser.DepartmentPositions
.Where(x => x.PositionType == PositionType.Manager)
.Select(x => x.Department.Id)
.Intersect(fooUser.DepartmentPositions.Select(x => X.Department.Id))
.Any()