我需要为以下场景编写一个连接查询。当存在具有主键的表而其他表将其定义为外键时,我知道如何编写连接。但这里的情况不同。请看表的结构。
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);