title: How can I convert Linq results to DTO class object without iteration
body: <p>I'm building a Web API project that will be made available to third-party's and also used by my own web application/s. The Web API methods will return JSON representations of complex types/objects. These are pre-defined classes which I can make available to third-party's so they understand how the data is structured and can deserialize the JSON. I'll refer to these classes as DTO classes until someone corrects me. </p>
  
  <p>I have the following auto-generated entity model (from database) well this is the User class anyway with a relationship to the Scan table (relationship can be ignored for the purpose of this question)...</p>
  
  <pre><code>public partial class User
  {
      public User()
      {
          this.Scans = new HashSet&lt;Scan&gt;();
      }
  
      public int Id { get; set; }
      public string Username { get; set; }
      public string Password { get; set; }
      public bool Active { get; set; }
  
      public virtual ICollection&lt;Scan&gt; Scans { get; set; }
  }
  </code></pre>
  
  <p>And the following DTO class which I would like to return to the presentation layer as List (I don't think I should use IEnumerable for business to presentation?).</p>
  
  <pre><code>public class User
  {
      public int Id;
      public string Username;
      public string Password;
      public bool Active;
  }
  </code></pre>
  
  <p>My business layer for this component currently looks like below. Using Linq to get users  from the database and then parsing the results and returning a List&lt;> of POCO Users.</p>
  
  <pre><code>public List&lt;User&gt; Get()
  {
      List&lt;User&gt; userList = new List&lt;User&gt;();
  
      using (var db = new MyContext())
      {
          var query = from u in db.Users
                      orderby u.FirstName
                      select u;
  
          foreach (var item in query)
          {
              User user = new User();
              user.Id = item.pkUser;
              user.Username = item.Username;
              user.Password = item.Password;
              user.Active = item.Active;
  
              userList.Add(user);
          }
      }
  
      return userList;
  }
  </code></pre>
  
  <p>Parsing through the results like this seems inefficient, and I have seen that you can do this sort of thing in the Linq query without iteration (e.g. the answer in this question <a href="https://stackoverflow.com/questions/9333005/mapping-linq-query-results-to-a-dto-class">Mapping Linq Query results to a DTO class</a>).</p>
  
  <p>I'm unsure as to where/how I should implement IEnumerable to enable this type of Linq query on my objects, and am also unsure of how to write a query similar to the one in the referenced question above but for my much simpler scenario.</p>
  
  <p>Thanks in advance for any help.</p>
  
  <p>P.S. Please ignore the fact that I am exposing Username and Password via Web API and also that I am returning all Users at once. Above is a simplified version of my code for the purpose of this question.</p>