title: Encapsulate LINQ query for different Repositories to avoid repeated code
body: <p>I have actually this tree method in my app:</p>
  
  <pre><code>public decimal GetPayrunAllowanceYTDValue(Guid employeeId, Guid payrunId, Guid payItemId, DateTime paymentDate)
  {
      var payrunPayItem = _firstRepository.FilterBy(x =&gt; x.EmployeeId == employeeId
                                                      &amp;&amp; x.PayrunsId != payrunId
                                                      &amp;&amp; x.PayItemId == payItemId
                                                     &amp;&amp; x.EffectiveDate &lt;= paymentDate, null)
                      .OrderByDescending(x =&gt; x.EffectiveDate).FirstOrDefault();
           return payrunPayItem.YTD ?? 0;
  }
  
  public decimal GetPayrunDeductionsAndSupersYTDValue(Guid employeeId, Guid payrunId, Guid payItemId, DateTime paymentDate)
  {
     var payrunPayItem = _secondRepository.FilterBy(x =&gt; x.EmployeeId == employeeId
                                                    &amp;&amp; x.PayrunsId != payrunId
                                                    &amp;&amp; x.PayItemId == payItemId
                                                    &amp;&amp; x.EffectiveDate &lt;= paymentDate, null)
                      .OrderByDescending(x =&gt; x.EffectiveDate).FirstOrDefault();
     return payrunPayItem.YTD ?? 0;
  }
  
  public decimal GetPayrunCompanyContributionYTDValue(Guid employeeId, Guid payrunId, Guid payItemId, DateTime paymentDate)
  {
     var payrunPayItem = _thirdRepository.FilterBy(x =&gt; x.EmployeeId == employeeId
                                                   &amp;&amp; x.PayrunsId != payrunId
                                                   &amp;&amp; x.PayItemId == payItemId
                                                   &amp;&amp; x.EffectiveDate &lt;= paymentDate, null)
                           .OrderByDescending(x =&gt; x.EffectiveDate).FirstOrDefault();
     return payrunPayItem.YTD ?? 0;
  }
  </code></pre>
  
  <p>As you can see, this 3 functions are exactly the same with only one difference, they are querying to different repositories. I'm trying to refactor this to some generic function, like this one:</p>
  
  <pre><code>public decimal GetYtdValue&lt;T&gt;(T repository, Guid employeeId, Guid payrunId, Guid payItemId)
              where T : IReadonlyRepository&lt;FirstRepositoryType&gt;, IReadonlyRepository&lt;SecondRepositoryType&gt;, IReadonlyRepository&lt;ThirdRepositoryType&gt;
  {
     var payrunPayItem = repository.FilterBy(x =&gt; x.EmployeeId == employeeId
                                                  &amp;&amp; x.PayrunsId != payrunId
                                      &amp;&amp; x.PayItemId == payItemId, null).OrderByDescending(x =&gt; x.EffectiveDate).FirstOrDefault();
              return (payrunPayItem.YTD ?? 0);
          }
  </code></pre>
  
  <p>The problem here is that I'm getting an error because there is an "Ambiguous reference" since all the repositories have the same base class with those properties.</p>
  
  <p>So, there is a way to refactor this and have a more elegant-not repeated code?</p>
  
  <p>Thanks in advance</p>