0

I have tables in the database named

Students (StudentId,Name,Address)
Subjects (SubjectId,SubName)
Stud_Subjects (StudentId,SubjectId)

And I hv created c# classes for Student and Subject. I want to take data from Stud_Subjects table do i need to create another class named "Stud_Subject". How to add add properties to that class.

lets say I want to get data joining these tables and the result should be like this.

(StudentId,Name,SubName)

How to map these result into C# class. do i have to create another class with above three fields.

2
  • Is that a mapping table Stud_Subjects? Post the table definition Commented Jul 20, 2014 at 16:25
  • @SriramSakthivel i updated the table definition Commented Jul 20, 2014 at 16:27

1 Answer 1

1

Two classes is enough. One for Student one for Subject. It looks like Stud_Subjects table is for "Many-Many relationship", you can implement that via a collection.

class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public List<Subject> Subjects { get; private set; }
}

class Subject
{
    public int SubjectId { get; set; }
    public string SubjectName { get; set; }

    public List<Student> Students { get; private set; }
}

Subjects collection in Student class will have all the subjects which a student is mapped and Students collection in Subject class will have all the students which is mapped to current Subject.

You could also consider converting List<T> to Dictionary<int, Student> and Dictionary<int, Subject> for easy access them via their Ids

Sign up to request clarification or add additional context in comments.

2 Comments

Thank u very very much dude.. I think it is the answer.
@Daybreaker Welcome, make sure you see my edit. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.