In our previous two articles we explained how to implement a One-To-One and a One-To-Many relationship in a C# class. You can read them here:
One to one relationship in C# class
Implement one to many relationship in C# class.
In this article we will learn to implement a Many-To-One relationship in a C# class. As in the previous two articles, we will explain the scenario where a Many-To-One relationship is relevant. Think about the situation where we want to represent a relationship between a teacher and a student. It is a very common scenario where one student is guided by many teachers. Another relationship is between a customer and the shop. Many customers may come into a shop for shopping. So, in those situations we can build a Many-To-One relationship.
We can think of a Many-To-One relationship as just the opposite of a One-To-Many relationship. Let's see a few examples and a practical implementation.
Relation between customer and shopkeeper
Here we will implement a relationship between customer and shop. Many customers may come into one shop, and it's a common scenario. Have a look at the following example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client
{
class Customer
{
Shop s = new Shop();
public string CName { get; set; }
public Shop GetSetShop
{
get
{
return s;
}
set
{
s = value;
}
}
public void Print()
{
Console.WriteLine("customer Name:- " + this.CName);
Console.WriteLine("shop Name:- " + this.GetSetShop.ShopName);
}
}
class Shop
{
public string ShopName { get; set; }
}
class Program
{
static void Main(string[] args)
{
Shop s = new Shop();
s.ShopName = "Computer World";
Customer a = new Customer();
a.CName = "Sourav";
a.GetSetShop = s;
Customer b = new Customer();
b.CName = "Ram";
b.GetSetShop = s;
a.Print();
b.Print();
Console.ReadLine();
}
}
}
Here is sample output. We are seeing that the customer name is different but the shop name is the same.
Implement relationship between teacher and student
In your school days you might have taken a lesson from more than one teacher. So you are the student of more than one teacher. It's nothing but a Many-To-One relationship. Have a look at the following code to learn the implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client
{
class Teacher
{
public string TName { get; set; }
public string subject { get; set; }
}
class Student
{
List<Teacher> Lst = new List<Teacher>();
public string SName { get; set; }
public List<Teacher> getsetTeacher
{
get { return Lst; }
set { Lst = value; }
}
public void Print()
{
Console.WriteLine("Student Name:- " + this.SName);
foreach (Teacher O in Lst)
{
Console.WriteLine("Teacher Name:- " + O.TName);
Console.WriteLine("Subject :- " + O.subject);
}
}
}
class Program
{
static void Main(string[] args)
{
List<Teacher> T = new List<Teacher>();
Teacher t1 = new Teacher();
t1.TName = "Ram sir";
t1.subject = "Math";
T.Add(t1);
t1 = new Teacher();
t1.TName = "shyam sir";
t1.subject = "computer";
T.Add(t1);
Student s = new Student();
s.SName = "Sourav";
s.getsetTeacher = T;
s.Print();
Console.ReadLine();
}
}
}
Here is sample output.
Conclusion
In this article we saw how to implement a Many-To-One relationship in a C# class. In the next article we will see how to implement a Many-To-Many relationship in a C# class. Follow this article series to learn more.