I'm having a problem with Mongodb querying.
Here's the classes:
class A
{
}
class B : A
{
}
class C : A
{
public string prop1 { get; set; }
}
class D
{
public List<A> collection1 { get; set; }
}
And the query:
var query1 = Query<D>.ElemMatch(d => d.collection1.OfType<C>(),
builder => builder.EQ(c => c.prop1, "val1"));
In a nutshell I want to select all instances of class D whose collection "collection1" contains at least one instance of class C whose property "prop1" equals "val1".
This keeps throwing "Unable to determine the serialization information for the expression" out of the blue.
What am I doing wrong? I've tried decorating "A" with BsonKnownTypes(typeof(B), typeof(C)) to no avail.
PS: if I had only one class A:
class A
{
public string prop1 { get; set; }
}
class D
{
public List<A> collection1 { get; set; }
}
then this code works and does exactly what I want:
var query1 = Query<D>.ElemMatch(d => d.collection1,
builder => builder.EQ(c => c.prop1, "val1"));
but it's not what I want, I want to keep "B" and "C" that inherit from "A".
Copyright Notice:Content Author:「user3664916」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26216185/mongodb-unable-to-determine-the-serialization-information