0

Insight.Database supports mapping One to Many results if the SQL has multiple SELECT statements, one for Parent and the for the children. See: https://github.com/jonwagner/Insight.Database/wiki/Specifying-Result-Structures

CREATE PROC GetBeerAndPossibleGlasses AS
SELECT * FROM Beer
SELECT b.BeerID, g.* FROM Beer JOIN Glasses ON (...)

class Beer
{
    public int ID;
    public IList<Glass> Glasses;
}

var results = connection.Query("GetBeerAndPossibleGlasses", Parameters.Empty,
    Query.Returns(Some<Beer>.Records)
        .ThenChildren(Some<Glasses>.Records);

Can the one-to-many mapping be done using a single join query? I believe this is supported in Petapoco. (I am using Auto Interface implementation but I would like to know if there is any way at all to accomplish it.) e.g.

CREATE PROC GetBeerAndPossibleGlasses AS    
SELECT b.*, g.* FROM Beer JOIN Glasses ON (...)

I have relatively complex query where I need to return parent and children as above. It works if I create multiple SELECT statments but my select statement look like below:

SELECT * FROM Beer WHERE <Complex WHERE clause>
SELECT b.BeerID, g.* FROM Beer JOIN Glasses ON (...) <Complex WHERE clause>

I am worried that the query is as efficient as a single join.

4

1 回答 1

1

看起来您想像这样返回单个记录集:

Beer=1, Glass=A
Beer=1, Glass=B

并将其转换为这些对象:

Beer {
    ID=1
    Glasses = {
         Glass { ID=A }
         Glass { ID=B }
    }
}

Insight 需要对啤酒进行重复数据删除,并将眼镜包装到父母身上。

目前不支持该场景,但将其添加为新类型的记录读取器(然后将其绑定到接口实现)不会太难。

根据您的用例,返回两个记录集中的数据可能会或可能不会更好/更快。两个记录集可能会再次扫描索引,但它们可能在内存中,并且可能比复制每个子记录中的数据更快。找出答案的唯一方法是分析您的数据。

如果您发现仍需要在单个记录集中返回数据,请在 github 站点上打开一个功能请求,我们将研究实现它。

于 2014-11-28T14:43:25.593 回答