title: How to include only some items of internal list with LINQ to Entities
body: <p>I have following entities (simplified for readability):</p>
  
  <pre><code>public class Country
  {
      public List&lt;NameLocalized&gt; Names;
  }
  
  public class NameLocalized
  {
      public string Locale;
      public string Value;
  }
  </code></pre>
  
  <p>I also have a <code>DbSet&lt;Country&gt; Countries</code> in my context. How to write a LINQ query which will return the list of countries but with filtered Names (so <code>Names</code> list will contain only single item based on specified locale. Something like this (however, this is not a working example)</p>
  
  <pre><code>public List&lt;Country&gt; GetCountries(string locale)
  {
      var result = context.Countries.Include(c =&gt; c.Names.Select(n =&gt; n.Locale == locale)).ToList();
      return result;
  }
  </code></pre>
  
  <p>How to achieve this in 2 steps if not possible in one</p>
  
  <pre><code>public List&lt;Country&gt; GetCountries(string locale)
  {
      //I have to use Include for eager loading
      var tempResult = context.Countries.Include(c =&gt; c.Names);
  
      var result = //Some code to convert tempResult to result, where only interesting locale is included
      return result;
  }
  </code></pre>
  
  <p>I am trying following code to remove unnecessary items, but it does not work too</p>
  
  <pre><code>result.ForEach(c =&gt; c.Names.ToList().RemoveAll(n =&gt; n.Locale != locale)); 
  </code></pre>
  
  <p>UPDATE:
  I have managed to remove items with following code (using extension method)</p>
  
  <pre><code>public static void RemoveAll&lt;T&gt;(this ICollection&lt;T&gt; collection,Predicate&lt;T&gt; predicate) 
  {
      if (predicate == null)
      {
          throw new ArgumentNullException("predicate");
      }
       collection.Where(entity =&gt; predicate(entity))
          .ToList().ForEach(entity =&gt; collection.Remove(entity));
  }
  
  
  result.ForEach(c =&gt; c.Names.RemoveAll(n =&gt; n.Locale != locale));
  </code></pre>
  
  <p>UPDATE2:</p>
  
  <p>With the help of answers and comments I was able to succeed. Details are at <a href="https://stackoverflow.com/questions/36343067/convert-entity-with-navigation-property-to-dto-using-iqueryable">Convert Entity with navigation property to DTO using IQueryable</a></p>