使用 LINQ 查询 db4o 中的嵌套类?

发布于 2024-10-18 21:27:48 字数 2254 浏览 1 评论 0原文

db4o 和简单的嵌套结构遇到了一个棘手的问题。

问题:当我使用 LINQ 从 db4o 中提取数据时,它提取了太多数据(即“where”子句似乎不起作用)。

我有一些嵌套对象:

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"

我创建两个交易品种:

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

然后创建一些交易日:

  // 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");

然后为每个交易品种分配几天:

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

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

然后将其保存到 db4o 对象数据库:

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

到目前为止非常简单。

我用“db4o Object Manager Enterprise”检查数据库,果然有两个符号,如果我单击每个符号,每个符号都包含两天。

现在,我执行 LINQ 查询来提取数据:

 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);
 } 

而且,发生了一些奇怪的事情:

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

尽管我指定它应该只返回“SPX”的数据,但它也从“IBM”返回数据。

我做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-10-25 21:27:48

您没有发布类的确切定义,但您目前正在您的类中执行笛卡尔积 Linq 查询 - 您正在创建每种可能的组合,而您实际上只想要“匹配”组合(因为您的模式中的交易日由于某种原因似乎与符号相关联)。相反,您应该做的是加入,例如:

  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))
  ...

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))
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文