title: generic repository pattern with joins
body: <p>I have begun exploring the repository pattern with entity framework (6.x) by reading what I consider the best basic treatment of the subject in this article: <a href="http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework.htm" rel="nofollow noreferrer">http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework.htm</a></p>
  
  <p>unfortunately it does not address joins. Using a generic repository, how does one join to an arbitrary number of other entities/repositories? </p>
  
  <p>as of now I have this the generic interface:</p>
  
  <pre><code>    interface IGenericRepository&lt;T&gt; where T: class
  {
      IEnumerable&lt;T&gt; SelectAll();
      T SelectByID(object id);
      void Insert(T obj);
      void Update(T obj);
      void Delete(object id);
      void Save();
  
  }
  
      public class GenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt; where T : class
  {
      private SocialMediaEntities db = null;
      private DbSet&lt;T&gt; table = null;
  
      public GenericRepository()
      {
          this.db = new SocialMediaEntities();
          table = db.Set&lt;T&gt;();
      }
  
      public GenericRepository(SocialMediaEntities db)
      {
          this.db = db;
          table = db.Set&lt;T&gt;();
      }
  
      public IEnumerable&lt;T&gt; SelectAll()
      {
          return table.ToList();
      }
  
      public T SelectByID(object id)
      {
          return table.Find(id);
      }
  
      public void Insert(T obj)
      {
          table.Add(obj);
      }
  
      public void Update(T obj)
      {
          table.Attach(obj);
          db.Entry(obj).State = EntityState.Modified;
      }
  
      public void Delete(object id)
      {
          T existing = table.Find(id);
          table.Remove(existing);
      }
  
      public void Save()
      {
          db.SaveChanges();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>perhaps the customer, customer order example is a good choice to illustrate. thanks.</p>
  
  <hr>
  
  <p>After asking my question I found another SO question whose answer led to the 
  following solution for me which works. Question is have I mixed idioms? Is this
  approach keeping within the generic repository pattern?</p>
  
  <p>(question was <a href="https://stackoverflow.com/questions/5010110/entityframework-join-using-join-method-and-lambdas">Entityframework Join using join method and lambdas</a>)</p>
  
  <pre><code>using (CommerceEntities ce = new CommerceEntities())
  {
       // the repository is Company
       var results = repository.SelectAll()
      .Join(ce.Orders,                          // target
      p =&gt; p.OrderID,                           // FK
      s =&gt; s.ID,                                // PK
      (p, s) =&gt; new { Order = s, Company = p }) // project result
      .Select(x =&gt; x.Company).ToList();         // select result
  } 
  </code></pre>
  
  <p>after studying the first answer given, it seems much more in the spirit of generic repository pattern.</p>