I have one MongoDB collection that stores documents of three different classes (A, B, C), which all inherit from the common class D.
Using the official C # driver, I inserted documents of all three types (A, B, C), and they all look correct with the _t discriminator, and their class maps are registered in my code.
If I issue a LINQ query, for example the following (using VB):
dim Result = database.GetCollection("mycol").AsQueryable(Of C).Where(some where clause)
If I count the results of this, I get the error "Element", the name of the element from class A does not match any field or property of class C.
Shouldn't the discriminator kick in the AsQueryable(Of C) code here? It seems that when I .Count Where clause, which refers to class C elements, applies to documents A, B and C.
I tried adding .OfType(Of C) without effect, tried to convert to a list first with .ToList , but I keep getting the same error. Any ideas?
As a background, my client code usually deals with objects like D. A, B, C share a lot of common properties inherited from D, on which I want to put indexes, so I put them in one collection. However, sometimes I need to refer directly to an object of type A, B, or C in special circumstances.