title: Entity Framework LINQ to SQL translation: how does it work?
body: <p>In my application the data in the database must never be deleted.</p>
  
  <p>Entities have a property, Deleted, which can be set to true or false.</p>
  
  <p>Entities which are virtually deleted can not go out from the database, they must not exist from the application perspective.</p>
  
  <p>I am managing this by creating GetAll- methods (for example GetAllUsers) in my data access layer. The methods return only entities which are flaged as NOT deleted (!Deleted), and all other methods (for example GetUserById) retrieved data with these methods.</p>
  
  <p>See the example below...</p>
  
  <pre><code>public IEnumerable&lt;User&gt; GetAllUsers()
  {
      return _dataContext.Users.Where(element =&gt; !element.Deleted);
  }
  
  public User GetUserById(string userName)
  {
      return GetAllUsers().FirstOrDefault(elt =&gt; elt.UserName.Equals(userName));
  }
  </code></pre>
  
  <p>This architecture is very good from the perpsctive of reliability because I'm sure that I always extract NOT deleted entities, but I'm afraid that this is not efficient.</p>
  
  <p>My question is very simple: is my application extracting all data from the User table (<code>select * from User</code>) each time a specific User is requested, or is Entity Framework smart enough to understand what I want and translates to a SQL query of something like: <code>select * from User where userName = @userName</code>?</p>
  
  <p>If I'm extracting all data from the database each time I need a single user, then how can I change my code so that it creates the correct query? I would like to mantain the centralization, so that I don't have to specify !Deleted in each LINQ query.</p>