业务逻辑:
我有三张桌子。在Referral
l和Referrer
表中ReferralInstance
,many to many
,Referra
与Referer
关系。向候选人提供推荐表格,填写工作申请表。用户发布的Referral
表单显示给与请求公司关联的所有Referrer(aka Employee)。此外,请求公司中的员工可以选择"Accept"
或"Reject"
该配置文件(此详细信息在实例表中捕获)。
需要的行为:
现在,当候选人根据特定条件(如下所述)添加推荐表格时,我想检查此推荐请求是否已存在。如果它已经存在,那么我会显示警告/弹出窗口。
条件1:
当候选人刚刚发布推荐请求时。那么ReferralInstance
表将是EMPTY,所以我需要检查CompanyId
,CandidateId
和SkillId
是否匹配,如果所有这三个匹配现有的DB记录我想要将hasPreviousRequest
设置为true
条件2:
候选人发布请求后SkillId
,CompanyId
和CandidateId
匹配。还有很多员工(属于那个被要求的公司"拒绝了他,但没有人接受,所以我想将hasPreviousRequest
设置为true
。
条件3:
当候选人发布推荐请求时,SkillId
,CompanyId
,CandidateId
匹配,但推荐人之一已接受他作业,在这种情况下我想设置集{ {1}}至hasPreviousRequest
。
以下是我的尝试:
false
在上面的尝试中,我做了两次相同的部分LINQ查询。要么我想要一个更好的LINQ来同时检查所有三个条件。如果没有,那么我想存储条件1中使用的LINQ查询,并在检查条件2时使用它。
如下所示:
[HttpPost]
public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
{
bool hasPreviousRequest = false;
var candidateId = User.Identity.GetUserId();
// CHCEKING CONDITION ONE
if (_context.Referrals
.Any(r => ((r.CandidateId == candidateId)
&& (r.CompanyId == viewModel.CompanyId)
&& (r.SkillId == viewModel.SkillId))))
{
// NOW CHECKING CONDITION TWO
if (_context.Referrals
.Any(r => ((r.CandidateId == candidateId)
&& (r.CompanyId == viewModel.CompanyId)
&& (r.SkillId == viewModel.SkillId))
&& r.ReferralInstances
.Any(e => (e.ReferrerId != null) && (e.ReferralStatus == "Accepted"))))
{
hasPreviousRequest = false;
}
else
hasPreviousRequest = true;
}
return Json(new { hasPreviousRequest = hasPreviousRequest });
}
修改
[HttpPost]
public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
{
bool hasPreviousRequest = false;
var candidateId = User.Identity.GetUserId();
var PartialLINQ = _context.Referrals
.Any(r => ((r.CandidateId == candidateId)
&& (r.CompanyId == viewModel.CompanyId)
&& (r.SkillId == viewModel.SkillId)));
if (PartialLiinq)
{
// SOMETHING LIKE THIS
if (PartialLinq
&& r.ReferralInstances
.Any(e => (e.ReferrerId != null) && (e.ReferralStatus == "Accepted"))))
{
hasPreviousRequest = false;
}
else
hasPreviousRequest = true;
}
return Json(new { hasPreviousRequest = hasPreviousRequest });
}
有4个属性:
答案 0 :(得分:1)
[HttpPost]
public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
{
bool hasPreviousRequest = false;
var candidateId = User.Identity.GetUserId();
// Do an outer join between the tables on ReferralID and select only a new Anonymous type that has referrerId
// and status. If no record found in ReferralInstances then set status to empty.
var result = (from r in _context.Referrals
join ri in _context.ReferralInstances on r.ReferralID equals ri.ReferralID into refsInst
where ((ri.CandidateId == candidateId) &&
(ri.CompanyId == viewModel.CompanyId) &&
(ri.SkillId == viewModel.SkillId))
from rs in refsInst.DefaultIfEmpty()
select new {ReferenceEquals = rs.ReferrerId, Status = rs == null ? "":rs.ReferralStatus})
.ToList();
// This covers third condition
if(result.Any(p => p.ReferrerId != null && p.Status == "Accepted"))
{
hasPreviousRequest = false;
}
// This covers first and second conditions. If nothing found in ReferralInstances, the status will be empty
if(result.Any() && result.All(p => p.Status != "Accepted"))
{
hasPreviousRequest = true;
}
return Json(new { hasPreviousRequest = hasPreviousRequest });
}