递归连接与 Linq



我正在使用LinqToDB来帮助查询SQLite数据库,但是,我遇到了一个问题,我需要在连接集合中进行连接。这是我到目前为止所拥有的。

var craftList = from c in db.GetTable<Craft>()
    join cP in db.GetTable<CraftProduct>() on c.ID equals cP.CraftID into cPS
    join cM in db.GetTable<CraftMaterial>() on c.ID equals cM.CraftID into cMS
    select new
    {
        Craft = c,
        CraftProducts = cPS,
        CraftMaterials = cMS
    };

我需要添加到组中 cPS 和 cMS,并让每个元素与另一个表连接。下面是一个示例查询,它显示了需要对 cMS 中的元素执行的操作。

var materialList = from cM in db.GetTable<CraftMaterial>()
    join i in db.GetTable<Item>() on cM.ItemID equals i.ID
    select CraftMaterial.Build(cM, i);

经过一番混乱之后,我能够弄清楚我需要做什么,我只需要做多个查询。

var craftList = from craft in db.GetTable<Craft>()
                join craftProduct in db.GetTable<CraftProduct>() on craft.ID equals craftProduct.CraftID into
                    craftProducts
                join craftMaterial in db.GetTable<CraftMaterial>() on craft.ID equals craftMaterial.CraftID into
                    craftMaterials
                select new
                {
                    Craft = craft,
                    CraftProducts = from craftProduct in craftProducts
                                    join item in db.GetTable<Item>() on craftProduct.ItemID equals item.ID
                                    select CraftProduct.Build(craftProduct, item),
                    CraftMaterials = from craftMaterial in craftMaterials
                                     join item in db.GetTable<Item>() on craftMaterial.ItemID equals item.ID
                                     select CraftMaterial.Build(craftMaterial, item)
                };

相关内容

  • 没有找到相关文章

最新更新