182 Object Relations

示例

One to One

Student.cs

using System;

namespace College
{
    public class Student
    {
        public int RollNo { get; set; }
        public string StudentName { get; set; }
        public string Email { get; set; }
        public Branch branch { get; set; } //contains reference to object of Branch class, that represents the branch that the current student belongs to.
    }
}

Branch.cs

using System;

namespace College
{
    public class Branch
    {
        public string BranchName { get; set; }
        public int NoOfSemesters { get; set; }
    }
}

Program.cs

using System;
using College;

namespace OneToOneExample
{
    class Program
    {
        static void Main()
        {
            //Student class's object
            Student student = new Student();
            student.RollNo = 123;
            student.StudentName = "Scott";
            student.Email = "scott@gmail.com";

            //one-to-one relation
            student.branch = new Branch();
            student.branch.BranchName = "Computer Science Engineering";
            student.branch.NoOfSemesters = 8;

            //display
            Console.WriteLine(student.RollNo);
            Console.WriteLine(student.StudentName);
            Console.WriteLine(student.Email);
            Console.WriteLine(student.branch.BranchName);
            Console.WriteLine(student.branch.NoOfSemesters);

            Console.ReadKey();
        }
    }
}

One to Many

Student.cs

using System;
using System.Collections.Generic;

namespace College
{
    /// <summary>
    /// Represents a student
    /// </summary>
    public class Student
    {
        public int RollNo { get; set; }
        public string StudentName { get; set; }
        public string Email { get; set; }
        public List<Examination> examinations { get; set; }
    }
}

Examination.cs

using System;

namespace College
{
    /// <summary>
    /// Represents examination attempted by the student
    /// </summary>
    public class Examination
    {
        public string ExaminationName { get; set; }
        public int Month { get; set; }
        public int Year { get; set; }
        public int MaxMarks { get; set; }
        public int SecuredMarks { get; set; }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using College;

namespace OneToManyExample
{
    class Program
    {
        static void Main()
        {
            //Create object of Student class
            Student student = new Student();
            student.RollNo = 1;
            student.StudentName = "Allen";
            student.Email = "allen@gmail.com";
            student.examinations = new List<Examination>();
            student.examinations.Add(new Examination() { ExaminationName = "Module Test 1", Month = 5, Year = 2021, MaxMarks = 100, SecuredMarks = 87 });
            student.examinations.Add(new Examination() { ExaminationName = "Module Test 2", Month = 7, Year = 2022, MaxMarks = 100, SecuredMarks = 70 });
            student.examinations.Add(new Examination() { ExaminationName = "Final Test", Month = 3, Year = 2024, MaxMarks = 100, SecuredMarks = 91 });

            //print
            Console.WriteLine("Roll No: " + student.RollNo);
            Console.WriteLine("Student Name: " + student.StudentName);
            Console.WriteLine("Email: " + student.Email);
            Console.WriteLine("EXAMINATIONS:");
            foreach (Examination exam in student.examinations)
            {
                Console.WriteLine(exam.ExaminationName + ", " + exam.Year + "-" + exam.Month + ", " + exam.SecuredMarks + "/" + exam.MaxMarks);
            }

            Console.ReadKey();
        }
    }
}

Many to One

Employee.cs

using System;
using System.Collections.Generic;

namespace Company
{
    /// <summary>
    /// Represents an employee of the organization
    /// </summary>
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string EmployeeName { get; set; }
        public string Email { get; set; }
        public Department dept { get; set; }
    }
}

Department.cs

using System;

namespace Company
{
    /// <summary>
    /// Represents a department of the organization
    /// </summary>
    public class Department
    {
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using Company;

namespace ManyToOneExample
{
    class Program
    {
        static void Main()
        {
            //Three employees in same department
            Employee employee1 = new Employee() { EmployeeID = 1, EmployeeName = "Scott", Email = "scott@gmail.com" };
            Employee employee2 = new Employee() { EmployeeID = 2, EmployeeName = "Allen", Email = "allen@gmail.com" };
            Employee employee3 = new Employee() { EmployeeID = 3, EmployeeName = "Smith", Email = "smith@gmail.com" };

            //create object of Department class
            Department department = new Department() { DepartmentID = 10, DepartmentName = "Accounting" };
            employee1.dept = department;
            employee2.dept = department;
            employee3.dept = department;

            //print
            Console.WriteLine("FIRST EMPLOYEE:");
            Console.Write("Employee ID: " + employee1.EmployeeID);
            Console.Write("Employee Name: " + employee1.EmployeeName);
            Console.Write("Email: " + employee1.Email);
            Console.Write("Department ID: " + employee1.dept.DepartmentID);
            Console.Write("Department Name: " + employee1.dept.DepartmentName);

            Console.WriteLine("\nSECOND EMPLOYEE:");
            Console.Write("Employee ID: " + employee2.EmployeeID);
            Console.Write("Employee Name: " + employee2.EmployeeName);
            Console.Write("Email: " + employee2.Email);
            Console.Write("Department ID: " + employee2.dept.DepartmentID);
            Console.Write("Department Name: " + employee2.dept.DepartmentName);

            Console.WriteLine("\nTHIRD EMPLOYEE:");
            Console.Write("Employee ID: " + employee3.EmployeeID);
            Console.Write("Employee Name: " + employee3.EmployeeName);
            Console.Write("Email: " + employee3.Email);
            Console.Write("Department ID: " + employee3.dept.DepartmentID);
            Console.Write("Department Name: " + employee3.dept.DepartmentName);

            Console.ReadKey();
        }
    }
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值