Using LINQ to query nested classes in db4o?

411 Views Asked by At

Run into a tricky problem with db4o and a simple nested structures.

Problem: when I use LINQ to pull data out of db4o, its pulling out too much data (i.e. the "where" clause doesn't seem to be working).

I have some nested objects:

Root ----Symbol (SPX)                
   |          |------Day 1: Date: 2010-10-18, string "SPX meta"
   |          |------Day 2: Date: 2010-10-19, string "SPX meta"
   |
   |
   |-----Symbol (IBM)               
              |------Day 1: Date: 2010-10-18, string "IBM meta"
              |------Day 2: Date: 2010-10-19, string "IBM meta"

I create two symbols:

  Symbol sym1 = new Symbol("SPX");
  Symbol sym2 = new Symbol("IBM");

I then create some trading days:

  // SPX
  TradingDay day1 = new TradingDay(new DateTime(2010, 10, 18), "SPX meta");
  TradingDay day2 = new TradingDay(new DateTime(2010, 10, 19), "SPX meta");

  // IBM
  TradingDay day3 = new TradingDay(new DateTime(2010, 10, 18), "IBM meta");
  TradingDay day4 = new TradingDay(new DateTime(2010, 10, 19), "IBM meta");

I then assign a couple of days to each symbols:

  sym1.AssignTradingDay(day1);
  sym1.AssignTradingDay(day2);

  sym2.AssignTradingDay(day3);
  sym2.AssignTradingDay(day4);

I then persist it to the db4o object database:

  // Store in the database.
  db4o.db.Store(sym1);
  db4o.db.Store(sym2);
  db4o.db.Commit();  

Pretty simple so far.

I check the database with "db4o Object Manager Enterprise", and sure enough, there is two symbols, and if I click on each symbol, each one contains two days.

Now, I do a LINQ query to pull the data out:

 var daysForSymbolS1 = from Symbol s in db4o.db
                       from TradingDay t in db4o.db
                       where (s.SymbolGlobal == "IBM" 
                              && t.Date == new DateTime(2010, 10, 19))
                       select new
                       {
                         s.SymbolGlobal,
                         t.Date,
                         t.Meta
                       };
 foreach (var d in daysForSymbolS1)
 {
   Console.WriteLine("{0} - {1} - {2}", d.SymbolGlobal, d.Date.Date, d.Meta);
 } 

And, something weird happens:

IBM - 10/19/2010 12:00:00 AM - SPX meta     // This line should not appear because we have specified "IBM" in the filter.
IBM - 10/19/2010 12:00:00 AM - IBM meta

Despite the fact that I specified that it should only return data for "SPX" its returning data from "IBM" as well.

Am I doing anything obviously wrong?

1

There are 1 best solutions below

1
On BEST ANSWER

You didn't post the exact definition of your classes but you are currently doing a cartesian product in your Linq query - you are creating every possible combination whereas you really only want "matching" combinations (since a trading day in your schema for some reason seems to be associated with a symbol). What you should do instead is a join, something like:

  from TradingDay t in db4o.db
  join Symbol s in db4o.db
  on s.SymbolGlobal equals t.SymbolGlobal 
  where (s.SymbolGlobal == "IBM" 
         && t.Date == new DateTime(2010, 10, 19))
  ...