EntityFramework repository template- how to write GetByID lamba within a template class?

Posted by FerretallicA on Stack Overflow See other posts from Stack Overflow or by FerretallicA
Published on 2010-05-31T06:12:52Z Indexed on 2010/06/03 10:34 UTC
Read the original article Hit count: 471

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:

internal interface IRepository<T> where T : class
{
  T GetByID(int id);
  IEnumerable<T> GetAll();
  IEnumerable<T> Query(Func<T, bool> filter);
}

GetByID is proving to be the killer. In the implementation:

public class Repository<T> : IRepository<T>,IUnitOfWork<T> where T : class
{
  // etc...
  public T GetByID(int id)
  {
    return this.ObjectSet.Single<T>(t=>t.ID == id);
  }

t=>t.ID == id is the particular bit I'm struggling with. Is it even possible to write lamba functions like that within template classes where no class-specific information is going to be available?

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework