-1
public class Person
{
    List<Contact> Contacts {get;set;}
}

public abstract class Contact
{
    string Value {get;set;}
}

public class Email : Contact
{
}

public class Chat: Contact
{
}

I have typed collection for Person.

How can I query mongodb for specific derived type (lets say Email) which has some specific value in Value field?

I can build query as Person.Contacts.Value = "someValue" , but this query returns result if "someValue" matches with any Chat.Value. What i need is Query must search in only Email.Value field and return result accordingly?

Thanking you in advance.

Alex
  • 37,502
  • 51
  • 204
  • 332
dspatil
  • 327
  • 2
  • 8

1 Answers1

3

The below should do it. The trick is to add the "_t" to the query. This can be done manually or, as show below, using OfType.

var emails = db.GetCollection<Contact>("contacts")
    .AsQueryable()
    .OfType<Email>()
    .Where(x => x.Value == "someValue");
Craig Wilson
  • 12,174
  • 3
  • 41
  • 45