title: Entity Framework Repository Pattern why not return Iqueryable?
body: <p>There are several good blogs about how to implement the repository pattern together with the unit-of-work pattern using generic classes.</p>
  
  <p><a href="http://codingandflying.com/2014/06/implementing-a-data-access-layer-with-entity-framework-6-1-part-2/" rel="noreferrer">Implementing a Data Access Layer with Entity Framework 6.1</a> </p>
  
  <p><a href="http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="noreferrer">Implementing the Repository and Unit of Work Patterns</a></p>
  
  <p>The Idea is, to define a generic interface IRepository and a class Repository that hides how the data is actually accessed. It can be accessed using Entity Framework DbContext, or maybe the repository is an in memory collection for unit testing.</p>
  
  <pre><code>public interface public interface IRepository&lt;T&gt; where T : class
  {
      T GetById(int Id);
      void DeleteById(int Id);
  
      void Add(T entity);
      void Update(T entity);
  
      etc.
  }
  </code></pre>
  
  <p>Quite often I see that several Query functions are added that are similar to Queryable and/or Enumerable functions.</p>
  
  <p>For instance in <a href="http://codingandflying.com/2014/06/implementing-a-data-access-layer-with-entity-framework-6-1-part-2/" rel="noreferrer">Implementing a data access layer</a> I see:</p>
  
  <pre><code>/// Returns an IEnumerable based on the query, order clause and the properties included
  /// &lt;param name="query"&gt;Link query for filtering.&lt;/param&gt;
  /// &lt;param name="orderBy"&gt;Link query for sorting.&lt;/param&gt;
  /// &lt;param name="includeProperties"&gt;Navigation properties seperated by comma for eager loading.&lt;/param&gt;
  /// &lt;returns&gt;IEnumerable containing the resulting entity set.&lt;/returns&gt;
  IEnumerable&lt;T&gt; GetByQuery(Expression&lt;Func&lt;T, bool&gt;&gt; query = null, Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy = null, string includeProperties = "");
  
  /// &lt;summary&gt;
  /// Returns the first matching entity based on the query.
  /// &lt;/summary&gt;
  /// &lt;param name="predicate"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  T GetFirst(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
  </code></pre>
  
  <p>If the interface had a function IQueryable GetQuery(), then I wouldn't have to make functions like GetFirst() and GetByQuery().</p>
  
  <blockquote>
    <p>Question: Why is this not recommended? Can people change the data in an undesirable way?</p>
  </blockquote>