none
How to do this by C# Linq RRS feed

  • Question

  • 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.


    Wednesday, January 29, 2020 9:24 PM

Answers

  • Hi zydjohn,

    Thank you for posting here.

    According to your description, I make a test on my side.

    Here's the code you can refer to:

            static void Main(string[] args)
            {
                List<Scores> binary_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);
                binary_credits.Add(score5);
                binary_credits.Add(score6);
                binary_credits.Add(score7);
                binary_credits.Add(score8);
                binary_credits.Add(score9);
                binary_credits.Add(score10);
    
                var totalCollection = binary_credits.AsEnumerable().GroupBy(x => new { x.CourseID, x.Student })
                .Select(x => new NewScores()
                {
                    CourseID = x.Key.CourseID,
                    Student = x.Key.Student,
                    productofcredits = x.Select(y => y.Credits).Aggregate((a, b) => a * b)
                }).OrderByDescending(x => x.productofcredits).GroupBy(x => x.CourseID).ToList();
    
                foreach (var eachCollection in totalCollection)
                {
                    foreach (var result in eachCollection)
                    {
                        Console.WriteLine($"CourseID: {result.CourseID} product of credits is: {result.productofcredits}");
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        public class NewScores
        {
            public int CourseID { get; set; }
            public int productofcredits { get; set; }
            public string Student { get; set; }
        }

    Result of my test:


    Hope it can help you.

    Best Reagrds,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by BonnieBMVP, Moderator Thursday, January 30, 2020 2:46 PM
    • Marked as answer by zydjohn Thursday, January 30, 2020 3:12 PM
    Thursday, January 30, 2020 11:38 AM
    Moderator

All replies

  • This is a path to where you want to get too, not a full solution by any means just a hard coded (use variables as needed)example to get a students credits.

    IEnumerable<int> bResults = binary_credits
        .Where(item => item.CourseID == 2 && item.Student == "A")
        .Select(item => item.Credits);
    
    If you want to do all all once than a GroupBy or a group by is needed.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, January 29, 2020 10:43 PM
    Moderator
  • Hello:

    Thanks for your reply, after testing, your code just return the credits for each student in the courses, but I want to get the product of the credits, which is the credits for each course multiplied each other.

    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;
    Actually, I know how to use Linq to find the Sum of credits for each students within the same CourseID, but my goals was to find how to get the Product (multiplied by each other credits with the same CourseID) and sort them from highest to lowest.

    Please advice!


    Thursday, January 30, 2020 9:37 AM
  • Hi zydjohn,

    Thank you for posting here.

    According to your description, I make a test on my side.

    Here's the code you can refer to:

            static void Main(string[] args)
            {
                List<Scores> binary_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);
                binary_credits.Add(score5);
                binary_credits.Add(score6);
                binary_credits.Add(score7);
                binary_credits.Add(score8);
                binary_credits.Add(score9);
                binary_credits.Add(score10);
    
                var totalCollection = binary_credits.AsEnumerable().GroupBy(x => new { x.CourseID, x.Student })
                .Select(x => new NewScores()
                {
                    CourseID = x.Key.CourseID,
                    Student = x.Key.Student,
                    productofcredits = x.Select(y => y.Credits).Aggregate((a, b) => a * b)
                }).OrderByDescending(x => x.productofcredits).GroupBy(x => x.CourseID).ToList();
    
                foreach (var eachCollection in totalCollection)
                {
                    foreach (var result in eachCollection)
                    {
                        Console.WriteLine($"CourseID: {result.CourseID} product of credits is: {result.productofcredits}");
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        public class NewScores
        {
            public int CourseID { get; set; }
            public int productofcredits { get; set; }
            public string Student { get; set; }
        }

    Result of my test:


    Hope it can help you.

    Best Reagrds,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by BonnieBMVP, Moderator Thursday, January 30, 2020 2:46 PM
    • Marked as answer by zydjohn Thursday, January 30, 2020 3:12 PM
    Thursday, January 30, 2020 11:38 AM
    Moderator