开发者

PetaPoco to return self-referencing hierarchy

开发者 https://www.devze.com 2023-03-30 22:31 出处:网络
How would one write a query/method to retur开发者_高级运维n a POCO that is from a self-referencing database as shown in this questionFirstly you would map it a flat class. eg. db.Fetch<CategoryDb&g

How would one write a query/method to retur开发者_高级运维n a POCO that is from a self-referencing database as shown in this question


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消