Retrieve all items that are in nested hierarchy

entity-framework entity-framework-core sqlite

Question

My app allows users to assign categories to their items. The classes look like this.

class Item
{
    public string Id { get; set; }
    /* ... */
    public virtual IEnumerable<Category> Categories { get; set; }
}

class Category
{
    public string Id { get; set; }
    public virtual Category Parent { get; set; }
    public virtual IEnumerable<Category> Subcategories { get; set; }
}

As you can see from the above there are are a hierarchy between categories.

If I have the following category tree:

|-- Item 1
|---- Child 1
|---- Child 2
|---- Child 3
|-- Item 2

And the user wants to display Item1, I want to include all the categories for Child 1, 2 and 3 in the result, i.e. four categories should be included (Item 1, Child 1, Child 2, Child 3) in the query.

How can I do this with EntityFrameworkCore. I'm using SQLite as a backend but would prefer to do this without SQL if possible.

1
2
8/8/2019 9:44:47 AM

Popular Answer

Have you tried using the ToString() method of DbFunction

ToString() will print the current object. So, It's children will also be printed. you need to override this method in Item class.

1
8/14/2019 7:29:30 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow