title: How to assign LINQ Query to a variable and then use it later in the code
body: <p><strong>Business Logic:</strong></p>
  <p>I have three tables. <code>Referral</code>, <code>Referrer</code>, <code>ReferralInstance</code> with <code>many to many</code> relationship in <code>Referra</code>l and <code>Referer</code> tables. A candidate is provided with a Referral form where he fills a Job Request. The <code>Referral</code> form is posted by the user is shown to all the Referrer (aka Employee) tied to the requested company. Also the Employee in request company can choose to  <code>&quot;Accept&quot;</code> or <code>&quot;Reject&quot;</code> that profile (this detail is captured in the instance table).</p>
  <p><strong>Behaviour Needed:</strong></p>
  <p>Now when candidate adds a Referral Form, based on certain condition (mentioned below) I want to check whether this referral request already exists. If it already exists then I show a warning/pop up.</p>
  <p>Condition 1:</p>
  <p>When candidate just now posted a referral request. Then <code>ReferralInstance</code> table will be EMPTY, so I need to check if <code>CompanyId</code>, <code>CandidateId</code> and <code>SkillId</code> matches, if all these three matches to an already existing DB Record I want to set <code>hasPreviousRequest</code>  to <code>true</code></p>
  <p>Condition 2:</p>
  <p>When candidate posted a request and <code>SkillId</code>, <code>CompanyId</code> and <code>CandidateId</code> matches. And many Employees (belonging to that requested company&quot; Rejected him, but no one has accepted then I want to set <code>hasPreviousRequest</code>  to <code>true</code>.</p>
  <p>Condition 3:</p>
  <p>When candidate posted a referral request, and <code>SkillId</code>, <code>CompanyId</code>, <code>CandidateId</code> matches but one of the Referrer has Accepted him for the Job, In that case I want to set set <code>hasPreviousRequest</code>  to <code>false</code>.</p>
  <p>Below is My Attempt:</p>
  <pre><code>[HttpPost]
  public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
  {
      bool hasPreviousRequest = false;
      var candidateId = User.Identity.GetUserId();
      // CHCEKING CONDITION ONE   
      if (_context.Referrals
          .Any(r =&gt; ((r.CandidateId == candidateId)
                         &amp;&amp; (r.CompanyId == viewModel.CompanyId)
                         &amp;&amp; (r.SkillId == viewModel.SkillId))))
      {
      // NOW CHECKING CONDITION TWO
          if (_context.Referrals
         .Any(r =&gt; ((r.CandidateId == candidateId)
                        &amp;&amp; (r.CompanyId == viewModel.CompanyId)
                        &amp;&amp; (r.SkillId == viewModel.SkillId))
                        &amp;&amp; r.ReferralInstances
                        .Any(e =&gt; (e.ReferrerId != null) &amp;&amp; (e.ReferralStatus == &quot;Accepted&quot;))))
          {
              hasPreviousRequest = false;
          }
          else
              hasPreviousRequest = true;
  
      }
  
      return Json(new { hasPreviousRequest = hasPreviousRequest });
  }
  </code></pre>
  <p>In above attempt, I made same partial LINQ Query twice. Either I want a better LINQ to check all the three condition at once. If not then I want to store LINQ Query used in Condition 1 and use it while checking Condition 2.</p>
  <p>Something like below:</p>
  <pre><code>[HttpPost]
      public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
      {
          bool hasPreviousRequest = false;
          var candidateId = User.Identity.GetUserId();
  
          var PartialLINQ = _context.Referrals
              .Any(r =&gt; ((r.CandidateId == candidateId)
                             &amp;&amp; (r.CompanyId == viewModel.CompanyId)
                             &amp;&amp; (r.SkillId == viewModel.SkillId)));
  
          if (PartialLiinq)
          {
            // SOMETHING LIKE THIS
                  if (PartialLinq
                            &amp;&amp; r.ReferralInstances
                            .Any(e =&gt; (e.ReferrerId != null) &amp;&amp; (e.ReferralStatus == &quot;Accepted&quot;))))
              {
                  hasPreviousRequest = false;
              }
              else
                  hasPreviousRequest = true;
  
          }
  
          return Json(new { hasPreviousRequest = hasPreviousRequest });
      }
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p><code>ReferralInstances</code> has 4 properties:</p>
  <ol>
  <li>Id PK 2. ReferrerId FK 3. ReferralID FK 4. ReferralStatus</li>
  </ol>