Implement One-To-Many Relationship in C# Class

Implement One-To-Many Relationship in C# Class

As the name suggests, in this article we will learn how to implement a One-To-Many relationship in a C# class. This is very common in project development. When I began to learn this concept from the .NET community website I did not find a very good article on this topic where an example has provided very prominently. That happened because I am a bad Googler (where a Googler is someone that searches using Google). So after a long day, suddenly this topic came to me and I decided to write a few articles regarding relationships of C# classes.

Ok, so this is the background and introduction. Let's start our topic. The question may come to mind of where we can implement One-To-Many relationships. When is it useful? If you are an experienced developer then I wish you aware of a few situations from your previous projects and if you are a very fresh beginner then you are wondering what the answer is from your bookish knowledge. Proceed with your thinking and I am giving my thoughts on the following.

Why One-To-Many relationship?

There are many scenarios where we can implement a One-To-Many relationship between two classes. For example, think about a person and a vehicle class. One person may be the owner of more than one vehicle. There might be a situation of representing a relationship between a person and a bank account. It is obvious that one person may own more than one bank account. An actual example from our family is that one parent might have many children. The relationship breaks due to a One-To-Many relationship.

So, those are the scenarios where we need to implement a One-To-Many relationship between classes.  So, without wasting time let's implement a few quick examples.

Relationship between Person and Vehicle

As in the previous explanation, the relationship between a person and their vehicles might (yes might, because many people have a single vehicle) be an example of a One-To-Many relationship. We have created two classes called Person and Car. Then we are assigning a list of Car objects to get and set a property of the person class. Hence the relationship is being created. Have a look at the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClientApp
{
    class Person
    {
        List<Cars> Lst = new List<Cars>();
        public string Name { get; set; }
        public string Surname { get; set; }
        public List<Cars> GetSetCar
        {
            get
            {
                return Lst;
            }
            set
            {
                Lst = value;
            }
        }
        public void Print()
        {
            Console.WriteLine("Name:- " + this.Name);
            Console.WriteLine("Surname:- " + this.Surname);
            Console.WriteLine("Car Owns");
            foreach (Cars c in this.GetSetCar)
            {
                Console.WriteLine("Car Name:- " + c.CarName);
            }
        }
    }
    class Cars
    {
        public string CarName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Cars> List = new List<Cars>();
            Cars a = new Cars();
            a.CarName = "Alto";
            Cars b = new Cars();
            b.CarName = "Namo";
            List.Add(a);
            List.Add(b);
            Person p = new Person();
            p.Name = "sourav";
            p.Surname = "Kayal";
            p.GetSetCar = List;
            p.Print();
            Console.ReadLine();
        }
    }
}

Here is sample output. At first we are creating two car objects and then we are combining it with a person object.

Relationship in C#

Implement mother-children relationship

It is another example of a One-To-Many relationship. Here we will implement a relationship between mother and children. The implementation is very similar to the previous one but with a few changes in the Main() function.  Have a look at the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Client
{
    class Mother
    {
        List<childerns> cname = new List<childerns>();
        public string Name { get; set; }
        public List<childerns> GetSetChilden
        {
            set { cname = value; }
            get { return cname; }
        }
        public void Print()
        {
            Console.WriteLine("Mother's Name:- " + this.Name);
            Console.WriteLine("Children are:- ");
            foreach (childerns c in this.GetSetChilden)
            {
                Console.WriteLine(c.CName);
            }
        }
    }
    class childerns
    {
        public List<childerns> getSetName { get; set; }
        public string CName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<childerns> Lst = new List<childerns>();
            childerns a = new childerns();
            a.CName = "Rahul";
            Lst.Add(a);
            a = new childerns();
            a.CName = "Priyanka";
            Lst.Add(a);
            a.getSetName = Lst;
            Mother m = new Mother();
            m.Name = "Sonia";
            m.GetSetChilden = a.getSetName;
            m.Print();
            Console.ReadLine();
        }
    }
}

Here is sample output.

One to many Relationship

Conclusion

In this article, we have seen how to implement a One-To-Many relationship in a C# class. In a future article, we will implement a few more relationships among classes. Happy day.


Recommended Free Ebook
Similar Articles