在 C# 中显示分层数据

发布于 2024-09-19 03:00:10 字数 1835 浏览 4 评论 0原文

我正在尝试开发一个页面来按学科/老师显示学生/班级结果。

有些班级有包含学生的小组,有些班级只有学生。一门学科由一位或多位教师思考。这将来可能会改变,我希望保持设计尽可能灵活(即主题可能会分解为模块或单独的测试结果,课程可能会按年份进一步分组等)

我想在网格中显示最终结果我可以扩展并汇总以平均结果(Y 上的班级和 X 上的主题)。

我认为它有 3 个数据库表,如下所示:

**class_structure**
id (int) PK
description (varchar)
parent_id (int) FK to id

最高级别是班级(没有家长 ID),最低级别是学生。

**subject_structure**
id (int) PK
description (varchar)
parent_id (int) FK to id

最高级别是科目,最低级别是老师,

**results**
id (int) PK 
class_structure_id (int) FK
subject_structure_id (int) FK
date (datetime)
reult (int)

您对上述结构有何看法?我相信这给了我前进的灵活性?

在我的 asp.net 应用程序中,然后我查询最高的类结构级别并平均出低于该级别的所有结果(我最好在数据库中的存储过程中进行平均还是在应用程序中使用 LINQ?)

这会给我返回一组最高水平的结果。

然后我需要根据主题类显示这是一个表格(我正在考虑在 C# 中创建表格 html?)(即首先创建所有标题,然后尝试将结果放在正确的列中(我不是确定如何做到这一点))。

<table border="1" padding="2">
 <tr>
  <td></td>
  <td></td>
  <td colspan="2" align="center">Subj 1</td>
  <td colspan="2" align="center">Subj 2</td>
 </tr>

 <tr>
  <td></td>
  <td></td>
  <td>Teacher 1</td>
  <td>Teacher 2</td>
  <td>Teacher 1</td>
  <td>Teacher 2</td>
 </tr>
 <tr>
  <td rowspan="2">Class 1</td>
  <td>Student 1</td>
  <td>90</td>
  <td>55</td>
  <td>75</td>
  <td>100</td>
 </tr>
 <tr>
  <td>Student 2</td>
  <td>40</td>
  <td>95</td>
  <td>65</td>
  <td>39</td>
 </tr>
</table>

然后,当用户单击第一个班级时,这将展开并显示所有组(如果有,则显示学生)(将单击的单元格的 class_struct_id 传递给相同的方法/存储过程并获取这些结果),然后重绘带有额外列的表。

这是实现我想要做的事情的好方法吗?

I am trying to develop a page to display student/class results by subject/teacher.

Some classes have groups which contain students and some have only students. A subject is thought by one or more teachers. This may change in the future and I want to keep the design as flexible as possible (i.e. subjects might be broken into modules or into individual test results, classes might be further grouped by year etc)

I want to display the end results in a grid that I can expand and roll up to average out the results (class on the Y and subject on the X).

My though it to have 3 database tables as follows:

**class_structure**
id (int) PK
description (varchar)
parent_id (int) FK to id

The highest level would be the class (with no parent id) and the lowest level would be the student.

**subject_structure**
id (int) PK
description (varchar)
parent_id (int) FK to id

The highest level being the subject, lowest being the teacher

**results**
id (int) PK 
class_structure_id (int) FK
subject_structure_id (int) FK
date (datetime)
reult (int)

What do you think of the above structure? I believe this gives me flexibility going forward?

In my asp.net app then I query the highest class structure level and average out all the results below that (am I better to do this averaging in a sproc in the DB or use say LINQ in the app?)

This would give me back a set of results at the highest level.

Then I would need to display this is a table (i am thinking of just creating the table html in C#?) against the subject classes (i.e. create all the headers first and then try to place the result in the correct column (i am not sure how to do this though)).

<table border="1" padding="2">
 <tr>
  <td></td>
  <td></td>
  <td colspan="2" align="center">Subj 1</td>
  <td colspan="2" align="center">Subj 2</td>
 </tr>

 <tr>
  <td></td>
  <td></td>
  <td>Teacher 1</td>
  <td>Teacher 2</td>
  <td>Teacher 1</td>
  <td>Teacher 2</td>
 </tr>
 <tr>
  <td rowspan="2">Class 1</td>
  <td>Student 1</td>
  <td>90</td>
  <td>55</td>
  <td>75</td>
  <td>100</td>
 </tr>
 <tr>
  <td>Student 2</td>
  <td>40</td>
  <td>95</td>
  <td>65</td>
  <td>39</td>
 </tr>
</table>

Then when the user clicked on the first class, this would expand out and show all the groups (if any, students if not) (pass the class_structure_id of the clicked cell to the same method/sproc and get those results) and then redraw the table with an extra column.

Is this a good way to go about achieving what I am trying to do?

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

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

发布评论

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

评论(4

温柔嚣张 2024-09-26 03:00:10

1:) 分层数据不应显示在 中,应使用嵌套

      ; 代替。表格用于表格数据。

2:) 由于 c# 是一种面向对象的语言,我不会通过 Id 引用树中的对象。为什么不添加子对象的集合作为成员,示例代码模式是:

class TreeElement 
{
 public TreeElement Parent {get;}
 public IEnumerable<TreeElement> Children{get;};
 public AddChild(TreeElement element }
 public bool IsRoot { return Parent == null; }
 public bool IsLeaf { return Children.Length == 0; }
 public bool IsBranch {return !IsRoot && !IsLeaf; }
}

通常在树中您不想进行父级查找。因此,如果不需要,请保留所有父引用以避免无用的复杂性;)
尝试以如上所述的对象结构在数据库中插入内容。

3:) 根据您的模型进行键查找最终会在 SQL 中变得非常慢。您必须循环数据并在查询中使用 if/else 语句
SQLTeam 上有一个很棒的示例,介绍了如何做到这一点使用额外的谱系柱可以快速实现。

4:) 使用 linq 查询数据并生成结果。

1:) Hierarchical data should not be displayed in a <table>, use nested <ul> or <ol> instead. Tables are for tabular data.

2:) Since c# is an object oriented language, I would not reference the objects in the tree by an Id. Why not add a collection of child objects as a member a sample code pattern would be:

class TreeElement 
{
 public TreeElement Parent {get;}
 public IEnumerable<TreeElement> Children{get;};
 public AddChild(TreeElement element }
 public bool IsRoot { return Parent == null; }
 public bool IsLeaf { return Children.Length == 0; }
 public bool IsBranch {return !IsRoot && !IsLeaf; }
}

normally in a tree you don't want to do a parent lookup. So if not needed leave all the parent references to avoid useless complexity ;)
Try to insert stuff in the database in an object structure like above.

3:) Key lookup according to your model will go eventually very slow in SQL. You 'll have to loop trough data and use if/else statements in your query
On SQLTeam is a great sample on how to make that very fast with the use of an extra lineage column.

4:) Use linq to query and generate the results for your data.

风蛊 2024-09-26 03:00:10

如果您可以在应用程序中使用 Jquery,请考虑使用以下 Jquery 插件,它有很多功能。

http: //www.jstree.com/

JsTree 完全免费(与 jQuery 相同的许可 – 根据 MIT 许可证或 GNU 通用公共许可证 (GPL) 第 2 版的条款)- 如 jstree 网站所述。

If you can use Jquery in your application, then consider using the following Jquery plugin, which has got lot of features..

http://www.jstree.com/

JsTree is absolutely free (licensed same as jQuery – under the terms of either the MIT License or the GNU General Public License (GPL) Version 2) - As stated in the jstree website.

本王不退位尔等都是臣 2024-09-26 03:00:10

我建议使用下表:

Person
 - Person_ID (PK)
 - Type (student/teacher etc)
 - Name etc...

PersonGroups
 - PersonGroup_ID (PK)
 - Description etc..

Subjects
 - Subject_ID (PK)
 - Description etc...

Classes
 - Class_ID (PK)
 - Subject_ID (FK)
 - Description etc...

PersonClassMembership
 - Person_ID (FK)
 - PersonGroup_ID (FK)
 - Class_ID (FK)

PersonGroupMembership
 - PersonGroup_ID (FK)
 - Person_ID (FK)

有了这个,就可以足够灵活地向班级/小组添加更多成员,例如新教师或学生。

这绝不是最好的方法或“正确”的方法,它只是让你开始思考
规范化数据库,以便更轻松地创建可提供结果的查询。

I would suggest having the following tables:

Person
 - Person_ID (PK)
 - Type (student/teacher etc)
 - Name etc...

PersonGroups
 - PersonGroup_ID (PK)
 - Description etc..

Subjects
 - Subject_ID (PK)
 - Description etc...

Classes
 - Class_ID (PK)
 - Subject_ID (FK)
 - Description etc...

PersonClassMembership
 - Person_ID (FK)
 - PersonGroup_ID (FK)
 - Class_ID (FK)

PersonGroupMembership
 - PersonGroup_ID (FK)
 - Person_ID (FK)

With this it is flexible enough to add more memberships such as new teachers or students to a class/group.

This is by no means the best way to do it or the 'right' way, its simply to get you going and thinking about
normalising the database to make creating queries that give you the results more easily.

赢得她心 2024-09-26 03:00:10

我不同意你的桌子设计。通过递归将混合数据放入一张表中确实是一个坏主意。除非您需要在数据库中存储树状数据,否则不要这样做。根据您的问题,更好的设计:

  • 表 Students
  • 表 Teachers
  • 表 Classes
  • 表 subjectTypes(type_id,description) 英语、数学、体育等...
  • 表 subjects(class_id,teacher_id,subject_type_id)
  • 表 StudentDistribution(stu_id,subject_id)
  • 表 Score (stu_id,subject_id,分数)

I don't agree with your table design. Putting mixed data in one table with recursion is really a bad idea. Don't do this unless you need to store tree-like data in DB. According to your question, a better design:

  • table Students
  • table Teachers
  • table Classes
  • table SubjectTypes(type_id,description) English,math,sports,etc...
  • table Subjects(class_id,teacher_id,subject_type_id)
  • table StudentDistribution(stu_id,subject_id)
  • table Score(stu_id,subject_id,score)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文