【问题标题】:How to assign LINQ Query to a variable and then use it later in the code如何将 LINQ 查询分配给变量,然后在代码中稍后使用它
【发布时间】:2018-02-18 00:18:48
【问题描述】:

业务逻辑:

我有三张桌子。 ReferralReferrerReferralInstancemany to many 关系在Referral 和Referer 表中。向候选人提供了一份推荐表格,他在其中填写了工作请求。用户发布的Referral 表单显示给与所请求公司相关的所有推荐人(又名员工)。此外,请求公司的员工可以选择"Accept""Reject" 该配置文件(此详细信息在实例表中捕获)。

需要的行为:

现在,当候选人添加推荐表时,根据特定条件(如下所述),我想检查此推荐请求是否已经存在。如果它已经存在,那么我会显示一个警告/弹出窗口。

条件一:

当候选人刚刚发布推荐请求时。然后ReferralInstance 表将为空,所以我需要检查CompanyIdCandidateIdSkillId 是否匹配,如果所有这三个匹配到已经存在的数据库记录我想将hasPreviousRequest 设置为true

条件二:

当候选人发布请求并且SkillIdCompanyIdCandidateId 匹配时。很多员工(属于那个被要求的公司)拒绝了他,但没有人接受然后我想将hasPreviousRequest设置为true

条件3:

当候选人发布推荐请求时,SkillIdCompanyIdCandidateId 匹配,但其中一位推荐人已接受他的工作,在这种情况下,我想将 hasPreviousRequest 设置为 false .

以下是我的尝试:

[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 });
}

在上述尝试中,我进行了两次相同的部分 LINQ 查询。要么我想要一个更好的 LINQ 来一次检查所有三个条件。如果不是,那么我想存储条件 1 中使用的 LINQ 查询并在检查条件 2 时使用它。

如下所示:

[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 });
    }

编辑

ReferralInstances 有 4 个属性:

  1. Id PK 2. ReferrerId FK 3. ReferralID FK 4. ReferralStatus

【问题讨论】:

  • 条件 1 中,您指的是 CoverLetterId - 应该是 SkillId `?
  • 是的,抱歉打错了
  • 在您的尝试中,您检查了 PartialLinq 两次。你不觉得第二次检查是多余的吗?
  • 这是我想要避免的。我不知道如何避免它。如何链接 LINQ 并仅检查 LINQ 的后半部分。
  • 您的第二个条件重复了第一个条件,看来您只需要检查ReferralInstances 表即可了解您的第二个条件。 ReferralInstances 表的属性是什么?

标签: c# asp.net-mvc linq lambda asp.net-mvc-5


【解决方案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 });
}

【讨论】:

  • 这个看起来可以解决问题。我现在就试试这个。出于好奇,您能否告诉我我在问题中发布的第一个代码 sn-p。 LOGIC WISE 有什么缺陷吗?请您交叉检查一次。
  • 我相信它仍然应该工作,只是性能和糟糕的设计。你运行你的代码了吗?它没有给你结果吗?
  • 我仍在开发代码,还没有达到运行测试的目的。但我会尽快尝试您的解决方案.. :)
  • 好的,如果您还需要帮助,请告诉我 :)
  • 我们可以使用Lamda风格的LINQ而不是SQL风格吗?
猜你喜欢
  • 2022-08-20
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2019-02-25
相关资源
最近更新 更多