0
princedis created
I have to create a self referencing tree like category, subcategory entity which is similar to OrganizationUnit.
public class Category
{
[Key]
public int Id { get; set; }
[StringLength(40)]
public string Name { get; set; }
public int? ParentId { get; set; }
[JsonIgnore]
public virtual ICollection<Category> Children { get; set; }
[JsonIgnore]
public virtual Category Parent { get; set; }
}
Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)
3 Answer(s)
-
0
Your entity seems fine except it must be derived from Entity class and should not include Id (since Entity defines it).
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
I have a question regarding application service implementation of GetCategories:
var categories = await _categoryRepository .GetAll() .Include(c => c.Children) .WhereIf(!input.Filter.IsNullOrEmpty(), c => c.DisplayName.Contains(input.Filter) ) .ToListAsync();it's dump all the database, how to avoid that? I want to load only parent category with childrens of this parent
Thank you.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Hi,
I think, it is really hard to write query of what you are asking with LinQ. Instead of this, get all list of categories and process them to create a tree in your app service.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)