c# - Why doesn't AsQueryable<T> imply a filter on the _t discriminator -


assuming model of

[bsondiscriminator(rootclass = true)] [bsonknowntypes(typeof (dog), typeof (cat))] class animal {     public objectid id {get;set;}     public string name {get;set;} } class dog : animal { public int barkvolume {get;set;} } class cat : animal { public int purrvolume {get;set;} } 

i can following:

var collection = new mongoclient().getserver().getdatabase("test").getcollection<animal("animals"); collection.save( new dog { name="spot", barkvolume=7 } ); collection.save( new cat { name="kitty", purrvolume=2 } ); 

however if try , query cats with

var cats = collection.asqueryable<cat>(); foreach(var c in cats) {    console.writeline("{0} purrs @ {1}", c.name, c.purrvolume); } 

i'll exception "element barkvolume not match field or property of class cat".

of course, if change query to: var cats = collection.asqueryable<cat>().where(x=>x cat);

then no problem, there warning stating x cat true.

is there particular reason why driver doesn't inject test on discriminator _t

it's design decision (which after working time, agree with). specify _t filter can use oftype extension method cleaner x => x cat.

in mongodb c# driver there typed , untyped options everything. typed options comfort, don't change resulting queries. thing when care query performance , index utilization.

for example if query using property specific type has don't need add oftype (and resulting _t filter) , if query engine might use _t index might not want do.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

qt - How to embed QML toolbar and menubar into QMainWindow -