.net - How to retrieve objects of an anonymous type from db4o -


i'd store objects of anonymous type db4o database. example:

// store object of anonymous type db var foobar = new {foo="ugh", bar="oh!"}; using (var db = db4oembedded.openfile("db.db40")) {     db.store(foobar); } 

i'm using following code retrieve objects:

// retrieve in separate program using (var db = db4oembedded.openfile("db.db40")) {     var query=from dynamic fb in db select fb;     query.dump(); } 

however, properties of object not accessible when after retrieval: dump gives (in linqpad) this:

5ienumerable<object> (3 items)   genericobject  (g) <>f__anonymoustype0`2[[system.string, mscorlib], [system.string, mscorlib]], query_vrfldn  genericobject  (g) <>f__anonymoustype0`2[[system.string, mscorlib], [system.string, mscorlib]], query_oqabew  genericobject  (g) <>f__anonymoustype0`2[[system.string, mscorlib], [system.string, mscorlib]], query_cfvuva  

is use case supported db4o? how objects neatly out of database?

anonymous types not officially supported db4o use care.

in sample code have 2 issues:

  1. using anonymous types
  2. accessing objects different assemblies

regarding 1, unfortunately, in order use linq db4o requires able reference type in code (which can't when using anonymous types). alternative use soda (note, sample code bellow works if code store/retrieves objects lives in same assembly)

using system; using db4objects.db4o;  namespace testanonymoustypes {     class program     {         static void main(string[] args)         {             var obj = new {name = "foo", id = "bar"};              if (args.length == 0)             {                 using (var db = db4oembedded.openfile("testanonymous.odb"))                 {                     db.store(obj);                 }                 return;             }              using (var db = db4oembedded.openfile("testanonymous.odb"))             {                 var query = db.query();                 query.constrain(obj.gettype());                  var result = query.execute();                  var y = result[0];                 console.writeline(y);             }         }     } } 

maybe possible extend db4o linq implementation allow 1 specify type dynamically (but not sure).

regarding 2, in sample tried use dynamic keyword. explained before, db4o needs actual type specified in linq expression, using dynamic not work. can use soda instead since db4o stores the assembly name (along side class name) when storing objects not work if have 2 different assemblies (since anonymous types defined in different assemblies).

a solution 2 have common assembly (which defines model) or playing aliasing.

best

adriano


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -