PetaPoco 返回自引用层次结构

发布于 2024-12-01 12:36:30 字数 133 浏览 1 评论 0原文

如何编写一个查询/方法来返回来自自引用数据库的 POCO,如 问题

How would one write a query/method to return a POCO that is from a self-referencing database as shown in this question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心旳酸楚 2024-12-08 12:36:30

首先,您将其映射为一个平面类。例如。 db.Fetch("select * fromcategories");

public class CategoryDb {
    public int Id { get; set; } 
    public string Name { get; set; }
    public int ParentCategoryId { get; set; }
}

从这里我将创建一个自引用自身的新对象。 (您可以使用具有 [Result] 属性的 ParentCategory 的现有对象。)

public class Category {
    public int Id { get; set; } 
    public string Name { get; set; }
    public Category ParentCategory { get; set; }
}

然后您可以将其转换为嵌套列表。
我确实在某处有可以执行此操作的代码,并且它还提供了搜索方法等,但它不在这台计算机上。我明天将更新代码链接。

Firstly you would map it a flat class. eg. db.Fetch<CategoryDb>("select * from categories");

public class CategoryDb {
    public int Id { get; set; } 
    public string Name { get; set; }
    public int ParentCategoryId { get; set; }
}

From here I would then create a new Object that self referenced itself. (You could use the existing object with the ParentCategory having the [Result] attribute on it.)

public class Category {
    public int Id { get; set; } 
    public string Name { get; set; }
    public Category ParentCategory { get; set; }
}

You could then take this and convert your flat list into a nested list.
I do have code somewhere that can do this, and for which it also provides searching methods etc, but its not on this computer. I will update tomorrow with a link to the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文