。包含不在父子条件中检索所需的值

时间:2015-12-03 07:17:53

标签: c# linq

我有employee table(ParentTable)和employee centers table(子表)。每个员工都分配不同的中心。当员工登录时,我想向所有员工显示 加入了他的中心。

例如: -

Center       Employee
 A,B,C          X
  B             Y
  C             Z

Employee X登录时,他应该能够查看Y & Z。但是当Employee Y登录时,不应查看Emp Z。{{1只应查看添加到Emp Y Center B的员工。

这是我尝试但无法获得理想结果

X & Y

2 个答案:

答案 0 :(得分:0)

因为1名员工可以帮助多个中心,我认为您应该使用交叉来检查

/* Checking condition  */  
_centerCodeIds.Intersect(x.EmployeeCenters.Select(c => c.Id))).Any()

测试:

var centerCodeIds = new List<int> { 1, 2, 3}; // X, Y, Z
var employeeCodeIds = new List<int> { 2, 4, 5}; // Z, A, B
centerCodeIds.Intersect(employeeCodeIds).Any(); // return true
centerCodeIds.Intersect(employeeCodeIds); // return 2

答案 1 :(得分:0)

我同意之前的回答者。使用Intersect可以为您提供所需的正确答案。您是否尝试过这样使用它?

List<int> _centerCodeIds = _cmn.GetCenterEmpwise(Convert.ToInt32(Session["LoggedUserId"]))
                           .Select(x => x.Id).ToList();

var _dTableEmployee
= _db.Employees
    .Where(x => x.Status == true && 
           x.EmployeeCenters.Select(x => x.Id).Intersect(_centerCodeIds))
    .OrderByDescending(x => x.Id)
    .Select(x => new
    {
        SlNo = "",
        Name = x.Name,
        CenterCode = x.EmployeeCenters
                 .Select(ec => ec.CenterCode.CentreCode)
                 .Aggregate((m, n) => m + "," + n),
        Designation = x.Designation.DesignationName,
        EmailId = x.OfficialEmailId,
        Mobile = x.OfficialMobileNo != null ? x.OfficialMobileNo : x.MobileNo,
        Id = x.Id
    });