Best way to do this is to utilize a parent-child relationship on the categories, S/O has a valid solution:
c# - ASP.Net MVC: How to show nested parent-child relation using recursive technique[
^]
From my experiences, if you have a lot of categories and depth experience performance issues; you may want to cache/persist the generated hierarchy.
Added: Moving the structure to the database
A very simple DB table for holding this information follows. You will probably want to add in descriptive information for these. Some people will also tell you to add in a Foreign Key on the ParentID with constraints... Generally this will not aid in performance within the DB itself, but if you are using an ORM such as Entity Framework it may help your application to better understand your database structure.
CREATE TABLE Categories (
CategoryID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
ParentID INT NULL,
CategoryName NVARCHAR(100) NULL,
)
GO
SET IDENTITY_INSERT Categories ON
INSERT Categories (CategoryID, ParentID, CategoryName)
VALUES (0, NULL, Root)
, (1, 0, 'Computer')
, (2, 0, 'Electronic')
, (3, 1, 'Lenovo')
, (4, 3, 'P250')
, (5, 2, 'Lise')
SET IDENTITY_INSERT Categories OFF
GO
I am not going to go into the C# side of this, as most users today do use an ORM of some sort while I am a straight ADO person.
What I can tell you is you are going to have a Category class that mirrors that DB structure, and you will need to have a list/collection of some sort to work with all of the categories.
To search for help on various items, I would suggest having
Parent Child Categories in your query