C# 层次类别结构

发布于 2024-08-15 05:16:54 字数 296 浏览 8 评论 0原文

我正在开发一个小型C# Windows应用程序,需要分层类别结构设计。我目前使用数据库中的单层类别,即没有子类别。我想允许用户创建多个级别的类别。我研究了这个线程类别的数据结构,但我在想是否有更简单的方法处理这类问题?因为我不确定这是否是解决问题的最佳方案。

如果有人可以提供数据库表结构和一些与之配套的 C# 代码,我将不胜感激。我还想检查是否能够从其父级获取所有子类别 ID(包括子子类别)。

I am developing a small C# windows application and in the need to Hierarchical Categories structure design. I am cuurrently using a single layer Categories from the DB i.e. no child categories. I would like to go about and allow the user to create multiple level categories. I looked into this thread Data structure for Category, but i was thinking if there is an easier way to deal with this sort of problem? because I am not sure if this would be the best solution for the problem.

I would appreciate if someone could provide the DB table structure and some C# code code to go with it. Also I wanted to check if I am able to get all child category ID's (including sub childs) from its parent.

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

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

发布评论

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

评论(5

魂牵梦绕锁你心扉 2024-08-22 05:16:54
create table Category
(
     id int primary key identity,
     parent_id int,
     name varchar(100),
     foreign key (parent_id) references Category (id)
)

public class Category
{
    private int id; 
    private string name;
    private Category Parent;
    private IList<Category> Children;
}

这是创建树的简单解决方案,当您重新调整对象层次结构时,该解决方案将需要一堆数据库选择。您还需要存储一些可以减少选择次数的信息,并记住树中节点的顺序。

Joe Celko 在写了很多内容 SQL 树比我能输入的任何内容都更有价值。我在查看“SQL 中的更多树和层次结构< /a>" 在 sqlteam.com 上

create table Category
(
     id int primary key identity,
     parent_id int,
     name varchar(100),
     foreign key (parent_id) references Category (id)
)

public class Category
{
    private int id; 
    private string name;
    private Category Parent;
    private IList<Category> Children;
}

That is a naive solution to create a tree, and when you're rehydrating your object hierarchy, that solution is going to require a bunch of DB selects. You need to also store some information that will reduce the number of selects, and remember the order of the nodes in the tree.

Joe Celko has written quite a bit on SQL trees that will be far more valuable than anything I can type. I found that link while looking at "More Trees & Hierarchies in SQL" on sqlteam.com

梦一生花开无言 2024-08-22 05:16:54

如果您的层次结构是刚性的并且不会改变,您可以对其进行硬编码。

该主题涉及流动且可能不断变化的层次结构,在这种情况下,所讨论的解决方案是合适的。

If your hierachy is rigid and will not change, you can hard code it.

The thread is about hierarchies that are fluid and may change constantly, and in that scenario the discussed solutions are appropriate.

如梦初醒的夏天 2024-08-22 05:16:54

Harvinder,

您是否考虑过使用 TreeView 控件来实现您的目的?我想这将是一个完美的主意。请查看有关 TreeView 控件的 MSDN 站点。 单击此处

Harvinder,

Have you considered using TreeView control for your purpose? I guess this would be a perfect idea. Please have a look at MSDN site regarding TreeView control. Click here.

葬花如无物 2024-08-22 05:16:54

Harvinder,

您正在考虑的数据库结构将完美地工作,但它有一些缺点:

  • 简单的sql查询不可能找到指定节点的所有子节点(您必须执行字符串操作)
  • 从内部不可能按子节点数量排序sql 查询
  • 来获取节点子节点的集合,您将必须多次连接到数据库(通过 ID 分别获取每个子节点)
  • 等。

最常见的做法是将单个 ID(父节点)存储在一列中。在这种情况下,多个子级可以指向同一个父级(一对多关系),这可以有效地解决您的问题。

你的问题的第二部分可以很容易地回答:除非你真的必须这样做,否则不要使用 Windows 窗体(两位作者都使用它) - 你会发现自己非常疲倦地将数据结构绑定到视图。更好的主意是使用 WPF 来实现您的目的,并灵活修改 Combobox 和 Treeview 数据模板以满足您的需求。如果您不熟悉 WPF,请首先查看此 关于 WPF 和 MVVM 设计模式的精彩文章 - 它甚至包含一个 Treeview 示例,这对您的情况很有帮助。

请告诉我我的回答是否解决了您的问题。我很乐意回答您有关 WPF 的任何问题。

Harvinder,

The database structure you're thinking about will work perfectly, but it has few disadvantages:

  • simple sql query finding all children of specified node is not possible (you would have to perform string operations)
  • sorting by number of children is impossible from within sql query
  • to get a collection of node's children you will have to connect to the database several times (to get every child separately by its ID)
  • etc.

The most common practice is to store single ID (parent) in one column. In this case multiple children can point at the same parent (relation one-to-many), which solves your problem efficiently.

The second part of your question can be answered easily: unless you really have to, don't use Windows Forms (both authors used it) - you will find yourself extremely tired binding your datastructure to your view. It is a far better idea to use WPF for your purpose and flexibly modify Combobox and Treeview Datatemplates to face your requirements. If you're not familiar with WPF please start by having a look at this brilliant article concerning WPF and MVVM design pattern - it does even contain a Treeview examples, which will be helpful in your case.

Please tell me if my answer solved your problem. I'd be happy to answer any of your questions concerning WPF.

尘世孤行 2024-08-22 05:16:54

感谢您的回复和考虑阅读我的问题。

我目前已经有了一个单一类别设置的结构,我知道这很简单。我正在考虑采取稍微简单的路线(我认为这是更简单的路线,但可能是错误的)。

我目前正在考虑在类别表中添加一个名为children_ids 的额外列。这样所有的父母都会记录孩子的情况,而不是相反。 Children_ids 列可以是文本类型,并且 ids 可以以字符串格式存储,即 1-4-5-7-8 等,一旦我从数据库中获取此列,我可以用“-”拆分字符串并获取所有内容其子代的 id。

我认为这样我会更容易关注所有人口;),只需向家长询问他们孩子的情况即可。我认为它也将减轻依赖性搜索,因为我猜我只需要更快地获得所有子级(递归地以下所有级别)的列表。这样我还可以在从数据库加载它们之前对所有整体进行排序,另一个令人头疼的问题消失了。

我确信必须有更好的解决方案,但不知道它是否会更容易。

我的另一个要求是创建一个具有此类别子父样式的下拉组合框,类似于用户通过它们进行选择的文件夹列表结构。也许类似于 CodeProject 示例CodeGuru 示例,我可能会使用其中一种方法来让我的生活更轻松一些。

问题是我想在下拉菜单的每个记录中添加更多详细信息,即 cat_id 等,但不希望它们对用户可见,这是为了获取有关用户选择的详细信息。我想我必须通过一个单独的 ArrayList 来补偿它,可能包含类别的所有详细信息,然后一旦用户从下拉列表中选择一条记录,就转到它的索引位置。我思维正常吗?

感谢您的阅读和回复!

Thanks for the replies and consideration for reading my question.

I currently already have a structure for a single category settings, pretty easy I know. I was thinking of taking a slightly simpler route (I think it is simpler route but might be wrong).

I am currently thinking of adding an extra column in the categories table called children_ids. So that all the parents will keep a record of there children rather than the other way around. I children_ids column can be of Text type and ids could be stored in a string format i.e. 1-4-5-7-8 etc. and once I get this column from the DB I can split the string with '-' and get all the ids for its children.

I think this way it will be slightly easier for me to keep an eye on all the population ;), just ask the parent about their children. I think it will also ease the dependency serches as well because I will only need to get a list of all the children (all levels below recursively) quicker I guess. This way I can also sort all the entires before loading them up from the DB, another headache gone.

I am sure there has to be better solutions out there, but don't know if it would be easier or not.

My other requirement was to create a dropdown combobox with this category child-parent style, similar to the folder list structure for the users to choose through them. Maybe something like CodeProject example or CodeGuru example, I might use either one of the approaches for make my life a little easier.

Th problem is I would like to add more details within each record of the dropdown menu i.e. cat_id etc but dont want them to be visible to the user, this is to get details on the user selections. I guess I will have to compensate it by haveing a seperate ArrayList maybe with all the details of the categories and then just go to its index location once the user selected a record from the dropdown list. Am I thinking straight?

Thanks for reading and for the replies!

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