【问题标题】:db4o: LINQ equivalent of SODA query?db4o:LINQ 相当于 SODA 查询?
【发布时间】:2011-02-23 18:36:16
【问题描述】:

对于 db4o,我正在尝试查找生成以下 SODA 的 LINQ 代码:

    var query = db4o.db.Query();
    query.Descend("Symbol");        
    query.Descend("_symbolGlobal").Constrain("APPLE");
    query.Descend("_date"); // Add a date constraint here.
    IObjectSet result = query.Execute();

SODA 所做的只是将树下拉到一个结束节点。例如,如果您想为日期“2010-10-18”选择“APPLE”,它将返回“Apples on Thursday”。

数据结构:

Root ----Symbol (APPLE)                
   |          |------Day 1: Date: 2010-10-18, string "Apples on Thursday"
   |          |------Day 2: Date: 2010-10-19, string "Apples on Friday"
   |
   |
   |-----Symbol (PEAR)               
              |------Day 1: Date: 2010-10-18, string "Pears on Thursday"
              |------Day 2: Date: 2010-10-19, string "Pears on Friday"

这是我的第一次尝试,因为它获得了叉积(即它查看了所有可能的组合),但它不起作用。我不能使用连接,因为 db4o 是一个对象数据库,您无法访问每个对象的 ID,就像在 RDBMS 中一样。

    var stringForDayAndSymbol = from s in db4o.db.Query<Symbol>(a => a.SymbolGlobal == "APPLE")
                          from t in db4o.db.Query<Date>(b => b.Date == new DateTime(2010, 10, 20))
                          select new
                          {
                            s.SymbolGlobal,
                            t.Date,
                            t.Meta
                          };

【问题讨论】:

    标签: linq db4o soda


    【解决方案1】:

    你真的想用query.Descend("Symbol"); 直接进入“符号”吗?我猜您想限制“符号”类型。我只是假设您的意思是:

    var query = db4o.db.Query();
    query.Constrain(typeof(Symbol));        
    query.Descend("_symbolGlobal").Constrain("APPLE");
    query.Descend("_date"); // Add a date constraint here.
    IObjectSet result = query.Execute();
    

    不是 100% 确定,但应该是这样的:

    // I assume here that the field _symbolGlobal is accessable with the property SymbolGlobal
    // and the field _date is accessable with the property Date
    var result = from Symbol s in db4o.db
                where s.SymbolGlobal == "APPLE"  
                select s.Date;
    

    【讨论】:

    • 我已将此答案标记为正确,但是,如果您尝试深入到对象集合中,例如使用 IList 实例化的任何对象,那么它将不起作用(集合似乎是SODA 看不见)。
    • 嗯,我认为集合上的“包含”操作也应该适用于 LINQ。像 s.SymbolGlobal.Contains("Apple")。
    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2023-03-10
    相关资源
    最近更新 更多