Linq的递归连接

时间:2015-06-15 22:57:35

标签: c# .net 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&amp; 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);

1 个答案:

答案 0 :(得分:0)

我能够弄清楚在搞乱之后需要做些什么,我只需要做多个查询。

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