title: How to properly return a subset of object properties
body: <p>I just started playing with the WebAPI template in VS2017 ASP.NET Core using Entity Framework Core, and I'm trying to figure out the best way to return a subset of an object's properties for a particular Get request. </p>
  
  <p>I used the built in scaffolding to generate a controller initially, and the <code>Get</code> request method it generated initially looked like this: </p>
  
  <pre><code>[HttpGet]
  public IEnumerable&lt;Person&gt; GetPeople()
  {
      return _context.People;
  }
  </code></pre>
  
  <p>My problem was that <code>Person</code> has a child class that I don't want included when someone makes a call to <code>GetPeople()</code>. </p>
  
  <p>Since I don't want to return an anonymous object, I instead added a stripped down class in the controller called <code>PersonInfo</code> that has only the properties I want to be returned, like this:</p>
  
  <pre><code>public class PersonInfo
  {
      public int  id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string AD { get; set; }
  }
  </code></pre>
  
  <p>and then I updated the <code>GetPeople()</code> method to this:</p>
  
  <pre><code>[HttpGet]
  public IEnumerable&lt;PersonInfo&gt; GetPeople()
  {
      List&lt;PersonInfo&gt; pi = new List&lt;PersonInfo&gt;();
      foreach(var person in _context.People
          .Select(p =&gt; new { p.id, p.FirstName, p.LastName, p.AD})
          .ToList())
      {
          PersonInfo newPerson = new PersonInfo();
          newPerson.id = person.id;
          newPerson.FirstName = person.FirstName;
          newPerson.LastName = person.LastName;
          newPerson.AD = person.AD;
          pi.Add(newPerson);
      }
      return pi;
  }
  </code></pre>
  
  <p>This works fine, it just feels horribly inefficient. There's got to be a better way, right? </p>