简体   繁体   English

LINQ 查询从由三个表连接的表中获取数据

[英]LINQ query to get data from a table connected by three tables away

I have three tables.我有三张桌子。

  1. organisationalUnit组织单位
  2. user用户
  3. Tasks任务

I want to return back data from the tasks table where the authorized user is in a particular department and the user too is in this department.我想从任务表中返回数据,其中授权用户位于特定部门,而用户也位于该部门。 For example, Sally is in the HR department.例如,Sally 在 HR 部门。 She wants to view Jim's tasks.她想查看 Jim 的任务。 Jim is also in the HR department with sally, therefore she is allowed to view his tasks. Jim 和 sally 也在 HR 部门,因此她可以查看他的任务。 However, she should not be able to view John's tasks as he is in the sales department.但是,由于 John 在销售部门,她应该无法查看 John 的任务。

organisationalUnit
--------------------
ID     |     name
--------------------
1      |     HR
2      |     Sales




      Users
----------------------------------------
ID     |     name     |  OUID(FK)
----------------------------------------
4      |     Jim      |  1
5      |     Sally    |  1
6      |     John     |  2



      Tasks
----------------------------------------------
ID     |     TaskInfo           |  userID(FK)
-----------------------------------------------
1      |     Task for Jim       |     4
2      |     Task for john      |     5
3      |     Task for Sally     |     6

current LINQ code当前的 LINQ 代码

public async Task<IEnumerable<TaskSchedule>> GetTasks(string RoleName)
{         
     // return back ou ID
     var OUObjReturned = _context.OrganisationalUnits.Where(o => o.Name == RoleName).ToList();
     var OUID = OUObjReturned[0].Id;

     // get users based on the OUID       
     var userObjects = _context.Users.Where(u => u.OUId == OUID).ToList();

     // returns back with an array of users that have the Organisational unit ID   
     var userIDFromObject = userObjects[0].Id;

     //need further code to query the task table to return back tasks based on userID


}

I am now stuck on what to do after I get back the list of users with their ids.我现在被困在我用他们的 id 取回用户列表后该怎么做。 Is there a better way to code up the LINQ query than the one I have done above?有没有比我上面所做的更好的方法来编写 LINQ 查询?

The LING query should be able to return back a list of tasks based on the organisationalUnit ID being sent into the query first. LING 查询应该能够根据首先发送到查询中的组织单位 ID 返回任务列表。 In the case above, sending the organisationalUnit ID of 1, should return back task row 1 and 2 as jim and sally are in OUID 1.在上述情况下,发送组织单位 ID 1,应该返回任务行 1 和 2,因为 jim 和 sally 在 OUID 1 中。

Try the below linq:试试下面的linq:

var roleName = "HR";
var tasks = (from t in _context.Tasks
            join u in _context.Users on t.UserId equals u.Id
            join o in _context.OrganisationalUnits on u.OUID equals o.Id
            where o.Name == RoleName
            select t).ToList();

Below is the Test:下面是测试:

public class OrganisationalUnit
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    [ForeignKey("OrganisationalUnit")]
    public int OUID { get; set; }
}

public class Task
{
    public int Id { get; set; }
    public string TaskInfo { get; set; }
    [ForeignKey("User")]
    public int UserId { get; set; }
}

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Result:结果:

在此处输入图片说明

public async Task<IEnumerable<TaskSchedule>> GetTasks(string RoleName)
{
   // Here you found the OU
   var OUObjReturned = await _context.OrganisationalUnits
    .Where(o => o.Name == RoleName)
    .FirstOrDefault();

   // Now get list of users id
   var userObjects = await _context.Users
    .Where(u => u.OUId == OUObjReturned.ID)
    .Select(u => u.ID)
    .ToListAsync();

   // Return Tasks
   return await _context.Tasks
    .Where(t => userObjects.Contains(t.UserID)
    .ToListAsync();

}

But there is other way: (Assuming that the FK are well configured) and the models included navigation properties Ex: User must include reference to organisationalUnit:但还有其他方式:(假设 FK 配置良好)并且模型包含导航属性例如:用户必须包含对组织单位的引用:

public class User
   {
      public int ID {get; set;}
      public string Name {get; set;}
      public OrganizationalUnit OU {get; set;}
      public int OUID {get; set;}
   }

Now you can query directly using the navigation properties see EF Net Core Relationships现在您可以直接使用导航属性进行查询,请参阅EF Net Core Relationships

public async Task<IEnumerable<TaskSchedule>> GetTasks(string RoleName)
{
   // Return Tasks
   return await _context.Tasks
    .Include(t => t.User)
    .Where(t => User.OU.Name == RoleName)
    .ToListAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM