PetaPoco 收藏属性
PetaPoco 中水合作为集合的 POCO 属性的正确方法是什么?我想做这样的事情,但不知道如何做。
return db.Fetch<ColorCategory, List<SubColor>, ColorCategory>((c, s) => { c.SubColors = *?*; return c; }, "SELECT [ColorCategory].[ColorCategoryID] " +
",[ColorCategory].[DisplayOrder] " +
",[ColorCategory].[Name] " +
",[ColorCategory].[ModifiedDate] " +
",[ColorCategory].[ModifiedUser] " +
",[SubColor].[SubColorID] " +
",[SubColor].[Name] " +
"FROM [ColorCategory] " +
"INNER JOIN [ColorCategorySubColor] ON [ColorCategory].[ColorCategoryID] = [ColorCategorySubColor].[ColorCategoryID] " +
"INNER JOIN [SubColor] ON [ColorCategorySubColor].[SubColorID] = [SubColor].[SubColorID]");
其中 ColorCategory 类有一个 List 属性 SubColors。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 sql 查询中加入 SubColors,这样您就可以获得每个 SubColor 的重复 ColorCategory 记录,如下所述:
http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships
官方方法(目前)是创建一个关系器类,其中包含一些状态(关于当前记录)和 PetaPoco 将为结果集中的每一行调用的方法。相关器告诉 PetaPoco 何时停止向 ColorCategory 添加子颜色并移至下一个父 poco。
然而,Schotime 添加了一些出色的功能可以自动执行此操作。
这在我的 GitHub 上的测试应用中进行了演示。编程非常简单,但请注意 SELECT 列的顺序至关重要,因为 PetaPoco 依赖于与 Fetch 中的流程相同的流程。波科列表。
另一种方法是 N+1 方法,您可以根据需要加载子颜色。
You can join the SubColors in the sql query, so you get back a duplicate ColorCategory record for every SubColor as explained here :
http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships
The offcial method (at the moment) is to create a relator class containing some state (about the current record) and a method which PetaPoco will invoke for every row in the resultset. The relator tells PetaPoco when to stop adding SubColors to the ColorCategory and move onto the next parent poco.
However, Schotime has added some brilliant functions that automate this.
This is demonstrated in my test app at GitHub. It's quite simple to program but be aware that the order of the SELECT columns is crucial as PetaPoco relies on the flow being the same as in the Fetch<T1, T2...> poco list.
The alternative is an N+1 approach where you load the SubColors on demand.