title: Populating a DTO class List<> with ENtity Framework query
body: <p>Is It possible to populate a List from within a EF query? My DTO is as follows:</p>
  
  <pre><code>public DTO()
  {
    public string ClientName { get; set;}
    public List&lt;string&gt; RelatedCodes { get; set;}
  }
  </code></pre>
  
  <p>My method to populate DTO = </p>
  
  <pre><code>Public DTO MyResult(string ClientCode)
  {
     return (from o in repository.RelatedClient 
             where o.LeadCode == ClientCode 
             select new DTO { ClientName = o.ClientName, RelatedCodes = o.RelatedCodes.ToList()});
  }
  </code></pre>
  
  <p>I know I shouldn't be adding the ToList() within the query or if I can I am doing it very wrong. Can anyone advise how this is done. My DTO is cut down for this example but I want to have lots of single properties and a List&lt;> (or several Lists) in a DTO with one Entity query if possible?</p>
  
  <p>So following answer below  I presume there isnt a way to query this direct. My original option would have been:</p>
  
  <pre><code>Public DTO MyResult(string ClientCode)
  {
           var temp = (from o in repositry.RelatedCode Where o.LeadCode == ClientCode select o).ToList();
  
            DTO dto = (from o in repository.LeadClient Where o.LeadCode == ClientCode Select o.ClientName).Firstordefault();
  
           foreach(string rc in temp)
              {    dto.RelatedCode.Add(rc);}  
           return dto;
  
    }
  </code></pre>
  
  <p>I am happy with this but would be grateful if someone could advise if this is the correct way to deal without something like automapper. I am trying to learn Entity Framework and want to check I am not missing some built in functionality.</p>