title: EntityFramework repository template - how to write GetByID lambda within a template class?
body: <p>I am trying to write a generic one-size-fits-most repository pattern template class for an Entity Framework-based project I'm currently working on. The (heavily simplified) interface is:</p>
  
  <pre><code>internal interface IRepository&lt;T&gt; where T : class
  {
    T GetByID(int id);
    IEnumerable&lt;T&gt; GetAll();
    IEnumerable&lt;T&gt; Query(Func&lt;T, bool&gt; filter);
  }
  </code></pre>
  
  <p>GetByID is proving to be the killer. In the implementation:</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt;,IUnitOfWork&lt;T&gt; where T : class
  {
    // etc...
    public T GetByID(int id)
    {
      return this.ObjectSet.Single&lt;T&gt;(t=&gt;t.ID == id);
    }
  </code></pre>
  
  <p>t=>t.ID == id is the particular bit I'm struggling with. Is it even possible to write lambda functions like that within template classes where no class-specific information is going to be available?</p>