Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:46:12+00:00 2026-05-15T22:46:12+00:00

I have a Dataset that contains tables from a university database, two(actually more) of

  • 0

I have a Dataset that contains tables from a university database,
two(actually more) of these tables contain a many to many relationship,
specifically I have a,

Students table (Contains information about students)
Courses table (Contains information about the courses)
TakesCourses table (Which is an intermediary table between the Students and Courses tables, every student can have many courses)

I want to input a “student ID” and fetch all the records from the Courses table according to the records found in TakesCourses table.

My code WORKS and it is as follows:

string stdInfo = string.Empty;

DataRow[] drStudent = null;
DataRow drCourses = null;
DataRow[] drStdCrs = null;

drStudent = universityDS.Tables["Students"]
    .Select(string.Format("StudentID='{0}'", txtStudentID.Text));

stdInfo += string.Format("Student {0} {1}:\nTaking Courses:",
    drStudent[0]["FirstName"].ToString().Trim(),
    drStudent[0]["LastName"].ToString().Trim());

drStdCrs = drStudent[0].GetChildRows(
    universityDS.Relations["FK_TakesCourses_Students"]);

//Can I optimize here? Is there a better way to code this
if (drStdCrs.Length > 0)
{
    for (int i = 0; i < drStdCrs.Length; i++)
    {
        drCourses = drStdCrs[i].GetParentRow(
            universityDS.Relations["FK_TakesCourses_Courses"]);

        stdInfo += string.Format("\nCourse: {0}",
            drCourses["CourseName"]);
    }               
}

MessageBox.Show(stdInfo);

My question is, how can I optimize the code after the comments?
is there a better way to write this?

(Note: I am a recovering developer, trying to refresh my skills)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:46:13+00:00Added an answer on May 15, 2026 at 10:46 pm

    I totally agree with JDoig. Here is an example of how your code might look like while using LINQ to SQL:

    string stdInfo = string.Empty;
    string studentId = txtStudentID.Text;
    
    using (var db = new CourseDataContext())
    {
        Student student = (
            from student in db.Students
            where student.StudentID == studentId
            select student).Single();
    
        stdInfo += string.Format("Student {0} {1}:\nTaking Courses:",
            student.FirstName, student.LastName);
    
        var courseNames =
            from taken in student.TakesCourses
            select taken.Course.CourseName;
    
        foreach (string courseName in courseNames)
        {
            stdInfo += string.Format("\nCourse: {0}", courseNames);
        }
    }
    
    MessageBox.Show(stdInfo);
    

    As you will see it takes far less code and does a much better job in showing the intent of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataset that returns questions and answers from the database, each answer
I need to find records in a dataset that have certain values from more
I created dataset that contains Table View from SqlServer 2008 database. Then I created
I have a VB.Net data set that contains data from multiple tables. Does anyone
I have generated a dataset that contains data spanning thirty days. Im trying to
i have the following linq query that create a left join between two tables:
I have a dataset that I read in from a complex xml structure.... here
I have a dataset that contains: Table { date itemName } The date for
I have an MS Access database that contains a table of dates, scores and
I have to add all the Id from the dataset that has Status value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.