使用数组的数组一个在C#中的许多关系 [英] Using Array of Arrays for One to Many Relationship in C#

查看:138
本文介绍了使用数组的数组一个在C#中的许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个控制台测试用C#。

I am making a console test with C#.

其实我从来没有使用C#,但VB.Net。我想对于一个一对多的关系创建阵列。

Actually I have never used of C# but VB.Net. I want to create arrays for one-to-many relationship.

我的一个是一个学生都有姓名,性别,......,进修课业。

My one is 'A Student' has 'Name','Sex',...,'Courses Taken'.

一个学生将采取很多课程,每门课程都有一个标题,其中包括主题。每个主题都有名称,描述和点。

A Student would take many course, each course has a Title and Included Subject. Each subject has Name, Description and Point.

筛选。

Student
- Name   - Sex   - Courses Taken

Take Courses
- Course Title   - Subject Included

Subject
- Subject Name             [Math]       [MVC]
- Subject description      [Advance]    [Building Website]
- Subject Point            [6.9]        [5.6]

我要存储阵列中的每个实体,但我不知道如何科目/课程连接到每个学生。我怎么能得到谁参加数学或MVC学生。因为每个学生可以有更多的则更多的当然/多个科目。

I want to store each entity in Arrays but I don't know how to connect subjects/courses to each Students. And how can I get Student who attending Math or MVC. Because every students can have more then more course/ more than one subjects.

推荐答案

您需要创建类来描述你的不同的对象。

You'll want to create classes to describe your different objects.

class Student
{
   string Name { get; set; }
   Gender Sex { get; set; } // write an enum for this
   IEnumerable<Course> CoursesTaken { get; set; }
}

class Course
{
   string Title { get; set; }
   Subject Subject { get; set; }
}

class Subject
{
   string Name { get; set; }
   string Description { get; set; }
   double Points { get; set; }
}

使用列表以创建这些新类型的实例枚举允许您使用LINQ来选择或评估列表的成员(嵌套的循环工作以及):

Using List to create enumerations of instances of these new types allow you to use LINQ to select or evaluate members of the list (nested for loops work as well):

// populate a list of students called studentList
//...
// use LINQ to select the students you want
var mathResults = studentList.Select(student => student.CoursesTaken.Any(course => course.Subject.Name == "Math"));

这篇关于使用数组的数组一个在C#中的许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆