我有以下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

此标准是:

如果经理的DepartmentPositionPositionTypeManager,并且该特定DepartmentPositionDepartment等于雇员的DepartmentDepartmentPositions,则经理为能够查看员工的绩效记录。

为此使用了一个规范类:

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必须持有标记为DepartmentPositionManager的条件。

最佳答案

假设部门有一个ID字段来唯一标识它。您应该可以执行以下操作:

return barUser.DepartmentPositions
         .Where(x => x.PositionType == PositionType.Manager)
         .Select(x => x.Department.Id)
         .Intersect(fooUser.DepartmentPositions.Select(x => X.Department.Id))
         .Any()

09-25 19:29