title: How to make generic function using LINQ?
body: <p>Based on information from <a href="https://blog.oneunicorn.com/2012/06/02/deleting-orphans-with-entity-framework/" rel="nofollow noreferrer">here</a>. </p>
  
  <p>I found how deleting orphans with Entity Framework.</p>
  
  <pre><code>public void SaveChanges()
  {
      context.ReportCards
          .Local
          .Where(r =&gt; r.Student == null)
          .ToList()
          .ForEach(r =&gt; context.ReportCards.Remove(r));
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>I was wondering how to make generic function for this part because it might be used often:</p>
  
  <pre><code>context.ReportCards
          .Local
          .Where(r =&gt; r.Student == null)
          .ToList()
          .ForEach(r =&gt; context.ReportCards.Remove(r));
  </code></pre>
  
  <p>I thought about something like this:</p>
  
  <pre><code>public void SaveChanges()
  {
     RemoveOrphans(Student, ReportCards) 
     context.SaveChanges();
  }
  
  private void RemoveOrphans&lt;T&gt;(T sourceContext, T orphan)
  {    
      context.orphan
          .Local
          .Where(r =&gt; r.sourceContext == null)
          .ToList()
          .ForEach(r =&gt; context.orphan
          .Remove(r));
  }
  </code></pre>
  
  <p>But of course it doesn't work. Any advice?</p>