我需要为下面的场景编写连接查询。我知道如果有表使用PRIMARY键而其他表将其定义为外键时如何编写联接。但这里的情况有所不同。请参阅表格的结构。
ID Type UserId
1 User 1 // Straight away defining User table
2 Dept 10 // Defining Dept. so it should get all the users under the Dept Id(10)
3 Route 100 // Defining Route. so it should get all the users from Route Id(100).
我不知道如何在LINQ中为这种情况编写连接查询。请给我一些建议。
我尝试了三组不同的查询,然后将这些结果作为单个列表加入。这是代码。
代码:
var usersWithDeptId = db.Users.Where(asd => asd.UserDeptId == Id).Select(asd => asd.Id);
var listUserValue = from cap in db.CareAllocationPercents
where cap.Type == 1 && UsersWithDeptId.Contains(cap.UserId)
select new UserWithDeptId
{
Year = db.CareAllocations.FirstOrDefault(amt => amt.CareItemId == cap.CareItemId).Year,
Amount = db.CareAllocations.FirstOrDefault(amt => amt.CareItemId == cap.CareItemId).Amount,
UserId = cap.UserId,
UserDeptId = db.Users.FirstOrDefault(userdept => userdept.Id == cap.UserId).UserDeptId,
Percentage = cap.Percentage,
CareItemId = cap.CareItemId,
Category = "User",
CareAllocationId = cap.Id
};
listUserValue = listUserValue.Where(asd => asd.Year == Year).ToList();
List<int> routeIds = db.CareAllocationPercents.Where(asd => asd.Type == 3).Select(asd => asd.UserId).ToList();
var UsersWithRoutingId = from route in db.RoutingListMembers
where RouteIds.Contains(route.RoutingListId.Value) && route.User.UserDeptId == Id
select new RoutingWithUser
{
UserId = route.UserId,
RoutingId = route.RoutingListId
};
var listRouteValue = from cap in db.CareAllocationPercents
where cap.Type == 3
select new UserWithDeptId
{
Year = db.CareAllocations.FirstOrDefault(amt => amt.CareItemId == cap.CareItemI).Year,
Amount = db.CareAllocations.FirstOrDefault(amt => amt.CareItemId == cap.CareItemI).Amount,
UserId = cap.UserId,
UserDeptId = db.Users.FirstOrDefault(userdept => userdept.Id == cap.UserId).UserDeptId,
Percentage = cap.Percentage,
Category = "Route",
CareItemId = cap.CareItemId,
CareAllocationId = cap.Id
};
List<UserWithDeptId> newRouteList = new List<UserWithDeptId>();
ListRouteValue = listRouteValue.Where(asd => asd.Year == Year).ToList();
foreach (var listdept in ListRouteValue)
{
var user = UsersWithRoutingId.FirstOrDefault(uwri => uwri.RoutingId == listdept.UserId);
if (user != null)
{
NewRouteList.Add(new UserWithDeptId
{
UserId = user.UserId,
Year = listdept.Year,
UserDeptId = db.Users.FirstOrDefault(asd => asd.Id == user.UserId).Select(asd => asd.UserDeptId),
Percentage = listdept.Percentage,
CareItemId = listdept.CareItemId,
Amount = listdept.Amount,
CareAllocationId = listdept.CareAllocationId,
Category = listdept.Category });
}
}
newRouteList = newRouteList.Where(asd => asd.UserDeptId == Id).ToList();
var listUserId = from user in db.Users
where user.UserDeptId == Id
select new UserWithDeptId
{
UserId = user.Id
};
var listDeptId = from cap in db.CareAllocationPercents
where cap.Type == 2 && cap.UserId == Id
select new UserWithDeptId
{
Year = db.CareAllocations.FirstOrDefault(amt => amt.CareItemId == cap.CareItemId).Year,
Amount = db.CareAllocations.FirstOrDefault(amt => amt.CareItemId == cap.CareItemId).Amount,
UserDeptId = cap.UserId,
Percentage = cap.Percentage,
Category = "Dept",
CareItemId = cap.CareItemId,
CareAllocationId = cap.Id,
};
listDeptId = listDeptId.Where(asd => asd.Year == Year).ToList();
List<UserWithDeptId> newList = new List<userWithDeptId>();
foreach (var listdept in listDeptId)
{
foreach (var users in listUserId)
{
newList.Add(new UserWithDeptId
{
UserId = users.UserId,
UserDeptId = listdept.UserDeptId,
Percentage = listdept.Percentage,
CareItemId = listdept.CareItemId,
Amount = listdept.Amount,
CareAllocationId = listdept.CareAllocationId,
Category = listdept.Category
});
}
}
newList.AddRange(listUserValue);
newList.AddRange(newRouteList);