How to do this by C# Linq

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
Hello:

I want to know how to do this by Linq.

See the following C# code:



public class Scores
{
public int CourseID { get; set; }
public string CourseName { get; set; }
public int Credits { get; set; }
public string Student { get; set; }
}
public static void Find_Max_Product_Credits()
{
List<Scores> binary_credits = new List<Scores>();
List<Scores> triple_credits = new List<Scores>();
Scores score1 = new Scores
{
CourseID = 2,
CourseName = "MBA 1",
Credits = 2,
Student = "A",
};
Scores score2 = new Scores
{
CourseID = 2,
CourseName = "MBA 2",
Credits = 3,
Student = "A",
};
Scores score3 = new Scores
{
CourseID = 2,
CourseName = "MBA 1",
Credits = 2,
Student = "B",
};
Scores score4 = new Scores
{
CourseID = 2,
CourseName = "MBA 2",
Credits = 2,
Student = "B",
};

Scores score5 = new Scores
{
CourseID = 3,
CourseName = "Math 1",
Credits = 3,
Student = "A",
};
Scores score6 = new Scores
{
CourseID = 3,
CourseName = "Math 2",
Credits = 4,
Student = "A",
};
Scores score7 = new Scores
{
CourseID = 3,
CourseName = "Math 3",
Credits = 5,
Student = "A",
};
Scores score8 = new Scores
{
CourseID = 3,
CourseName = "Math 1",
Credits = 3,
Student = "B",
};
Scores score9 = new Scores
{
CourseID = 3,
CourseName = "Math 2",
Credits = 6,
Student = "B",
};
Scores score10 = new Scores
{
CourseID = 3,
CourseName = "Math 3",
Credits = 9,
Student = "B",
};
binary_credits.Add(score1);
binary_credits.Add(score2);
binary_credits.Add(score3);
binary_credits.Add(score4);

triple_credits.Add(score5);
triple_credits.Add(score6);
triple_credits.Add(score7);
triple_credits.Add(score8);
triple_credits.Add(score9);
triple_credits.Add(score10);
}


What I want to do is:

I want to find the product of credits for the same CourseID of the same student, and sort the products of credits, then find those with the maximum of product of credits.

For example, for the CourseID = 2(MBA 1/MBA 2): Student ‘A’, his product of credits is: 2 * 3 = 6;

Student ‘B’, his product of credits is: 2 * 2 = 4;

For example, for the CourseID = 3(Math 1/Math 2/Math 3): Student ‘A’, his product of credits is: 3 * 4 * 5 = 60;

Student ‘B’, his product of credits is: 3 * 6 * 9 = 162;

Therefore, for CourseID = 2, Student ‘A’ has the maximum product of credits, which is 6; and for CorseID = 3, Student ‘B’ has the maximum product of credits, which is 162.

I can do this by multiple for loop, but since I have many different Courses (around 50), so I want find a better solution using Linq.

Please advice.

Continue reading...
 
Back
Top