Reputation: 21098
Given the following model
[BsonDiscriminator(RootClass = true)]
[BsonKnownTypes(typeof(Employee), typeof(Contractor)]
public class Person
{
public ObjectId Id {get;set;}
public string Name {get;set;}
}
public class Employee : Person
{
public double Salary {get;set;}
}
public class Contractor : Person
{
public double DailyRate {get;set;}
}
with the legacy driver I can do the following to get a list of all contractors.
var employees = database.GetCollection("people").AsQueryable<Employee>().OfType<Employee>();
How ever at the moment the AsQueryable() is not supported in the 2.0 driver (should be out for 2.1) so in the meantime, I'm at a bit of a loss on how to construct a suitable filter for selecting all contractors from the collection
var list = await collection.Find(filter).ToListAsync();
Upvotes: 1
Views: 743
Reputation: 12624
Relevant feature request here: https://jira.mongodb.org/browse/CSHARP-1194
For now, you can use an "is" filter.
collection.Find(x => x is Employee).ToListAsync();
You'll still need to cast at the end into an Employee, but they will all be filtered according to the registered discriminator.
Upvotes: 3