title: Entity Framework - query execution performance issue
body: <p>I'm working in a project ASP.NET MVC3 with Entity Framework on SQL Server with performance issues.</p>
  
  <p>Every time I load data from a view/table using EF and Linq. I can see by SQL Server Profiler that all the table/view content is retrieved because the where condition doesen't appear in profiler. Only later is filtered by LINQ i think. </p>
  
  <p>Is it correct? How to load only data I need at first on SQL Server?</p>
  
  <p>This is my example code:</p>
  
  <pre><code>var query = unitWork.City.GetFirstorDefault(item =&gt; item.City == cityCode);
  </code></pre>
  
  <p>Following an extraction of our datalayer with a data call example. Thanks for yours replies.</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork, IDisposable
      {
  
          #region CONSTRUCTOR
          public UnitOfWork()
          {
              _context = new  MyApplicationEntities(); //DataContext
              _context.ContextOptions.LazyLoadingEnabled = true;
              _context.ContextOptions.UseLegacyPreserveChangesBehavior = false; 
          }
          #endregion
  
         // DESC_RECHARGEABLE is a table in DB        
         public IGenericRepository&lt;DESC_RECHARGEABLE&gt; RepRechargeable
          {
              get{return _repRechargeable ?? (_repRechargeable = new GenericRepository&lt;DESC_RECHARGEABLE&gt;(_context));}
          }
  
  
  
  }
  
      public interface IGenericRepository&lt;T&gt; : ICollection&lt;T&gt;
          where T : class
      {
          IEnumerable&lt;T&gt; Query(Func&lt;T, bool&gt; predicate);
  
  void Update(T entity);
  
  T GetFirstorDefault(Func&lt;T, bool&gt; predicate);
  
  IEnumerable&lt;T&gt; GetAll();
  
  T GetByKey(Func&lt;T, bool&gt; predicate);
  
  bool Remove(T entity);
  
  void Add(T entity);
  
         ObjectSet&lt;T&gt; GetQuery();
      }
  
  
  public class GenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt;
          where T : class
      {
          private  MyApplicationEntities Currentcontext;
          public ObjectSet&lt;T&gt; entitySet;
          private List&lt;GenericRepository&lt;T&gt;&gt; _list = null;
          private string entityName;
  
          public GenericRepository( MyApplicationEntities context)
          {
              if (context == null)
                  throw new ArgumentNullException("context");
              this.Currentcontext = context;
              this.entitySet = context.CreateObjectSet&lt;T&gt;();
              this.entityName = entitySet.Name;
          }
  
          #region READ
          public IEnumerable&lt;T&gt; Query(Func&lt;T, bool&gt; predicate)
          {
              return this.entitySet.Where(predicate);
          }
  
  
          public T GetFirstorDefault(Func&lt;T, bool&gt; predicate)
          {
              return this.Query(predicate).FirstOrDefault();
          }
  
  
          public IEnumerable&lt;T&gt; GetAll()
          {
              return this.entitySet.AsEnumerable();
          }
  
          public T GetByKey(Func&lt;T, bool&gt; predicate)
          {
              return this.Query(predicate).FirstOrDefault();
          }
          #endregion
  }
  </code></pre>
  
  <p>//Here a client call example,load all DESC_RECHARGEABLE rows for a condition </p>
  
  <pre><code>var tempList = _unitofWork.RepRechargeable.Query(item =&gt; item.COMPANY_CODE == companyCode
                                                             &amp;&amp; item.DIVISION_CODE == divisionCode &amp;&amp; !string.IsNullOrEmpty(item.PROPERTY));
  </code></pre>