title: Filtering collection by _t on property
body: <p>I already implemented with mongo/c# a scenario where I store on the same collection different types of classes. Everything works great and I can do this in the end:</p>
  
  <pre><code>var collection = _mongoDb.GetCollection&lt;TLogType&gt;(CollectionNames.Logs).OfType&lt;TLogType&gt;();
  </code></pre>
  
  <p>Now I needed a similar functionality but for properties. Basically, I have collection (normal one, not polymorphic) which has an array of contacts. I wanted each item in the array to be of a specific contact type. The document I came up with and it is working looks like this (smaller for brevity):</p>
  
  <pre><code>Contacts" : [ 
      {
          "_t" : [ 
              "Contact", 
              "ExternalContact"
          ],
          "Role" : "Product Emergency",
          "Name" : "Fred",
          "Email" : "fred@gmail",
          "Phone" : "1231",
          "Availability" : "",
      }, 
      {
          "_t" : [ 
              "Contact", 
              "InternalContact"
          ],
          "Role" : "Manager",
          "Name" : "Mickey"
      }
  ]
  </code></pre>
  
  <p>What I want now is to retrieve contacts by type. We have the <code>OfType&lt;TResult&gt;</code> but it's only available to MongoQueryable. So, something like this:</p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;string&gt;&gt; GetContacts&lt;TContactType&gt;(CancellationToken token) where TContactType : Contact
  {
  
      var collection = _mongoDb.GetCollection&lt;Person&gt;(CollectionNames.Person)();
  
      var projectedListOfContacts =
          await collection.Find(
              s =&gt; s.Contacts != null &amp;&amp; 
              s.Contacts.OfType&lt;TContactType&gt; // how to filter by type on a property?
          )
              .Project(dto =&gt; dto.Contacts).
              .ToListAsync(token);
  
      ...
  } 
  </code></pre>
  
  <p>Any way to do this without having to get all contacts and work with them in memory? </p>
  
  <p>The relevant classes are:</p>
  
  <pre><code>public class Person
  {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public IEnumerable&lt;Contact&gt; Contacts {get; set;}
  }
  
  public abstract class Contact : IContact
  {
      public string Role { get; private set; }
      public string Name { get; private set; }
  
      protected Contact(string role, string name)
      {
          Role = role ?? throw new ArgumentNullException(nameof(role));
          Name = name ?? throw new ArgumentNullException(nameof(name));
      }
  }
  
  public class ExternalContact : Contact
  {
  
      public string Email { get; private set; }   
      public string Phone { get; private set; }   
      public string Availability { get; private set; }
  
      public ExternalContact(string role, string name, string email, string phone, string availability)
          : base(role, name)
      {
          Email = email ?? throw new ArgumentNullException(nameof(email));
          Phone = phone ?? throw new ArgumentNullException(nameof(phone));
          Availability = availability;
      }
  }
  </code></pre>
  
  <p><strong>Update:</strong> The answer helped me figure it out.. so in the end I could do this:</p>
  
  <pre><code>var contacts =
  await Collection.Find(s =&gt; s.Contacts != null)
      .Project(dto =&gt; dto.Contacts.OfType&lt;TContactType&gt;())
      .ToListAsync(token);
  </code></pre>
  
  <p>Turned out that the requirements changed.. and the Contact array will always have at least one of each type. So there was no reason to filter.. but I took advantage of the Projection that only returned the type I wanted. </p>