locked
How to use Linq to find max value for multiple columns RRS feed

  • Question

  • Hello,

    I have the following data (C#) program:

        public class Student
        {
            public int ID { get; set; }
            public int ClassCredit { get; set; }
            public int Grade { get; set; }
        }
    
        class Program
        {
            static void Main()
            {
                Student studentA1 = new Student
                {
                    ID = 1,
                    ClassCredit = 5,
                    Grade = 90,
                };
    
                Student studentA2 = new Student
                {
                    ID = 1,
                    ClassCredit = 5,
                    Grade = 80,
                };
    
                Student studentA3 = new Student
                {
                    ID = 1,
                    ClassCredit = 5,
                    Grade = 70,
                };
    
                Student studentB1 = new Student
                {
                    ID = 1,
                    ClassCredit = 10,
                    Grade = 90,
                };
    
                Student studentB2 = new Student
                {
                    ID = 1,
                    ClassCredit = 10,
                    Grade = 80,
                };
    
                Student studentB3 = new Student
                {
                    ID = 1,
                    ClassCredit = 10,
                    Grade = 70,
                };
    
                List<Student> students = new List<Student>();
                students.Add(studentA1);
                students.Add(studentA2);
                students.Add(studentA3);
                students.Add(studentB1);
                students.Add(studentB2);
                students.Add(studentB3);
            }
        }
    
    I want to find the element in the list with the maximum value for ClassCredit and Grade.
    In my above example, it is studentB1.
    I can use C# loop to do this, with Linq I can find the maximum value for only one column, but I don't know
    how to use Linq to find maximum value for both ClassCredit and Grade.
    Please advice,

    Thursday, August 13, 2020 7:15 PM

Answers

  • Probably you need something like this:

    Student result =

           students

                 .OrderByDescending( s => s.ClassCredit )

                 .ThenByDescending( s => s.Grade )

                 .FirstOrDefault( );

    (But what if there are several solutions?)


    • Edited by Viorel_MVP Thursday, August 13, 2020 7:28 PM
    • Proposed as answer by Naomi N Thursday, August 13, 2020 7:40 PM
    • Marked as answer by zydjohn Thursday, August 13, 2020 7:53 PM
    Thursday, August 13, 2020 7:25 PM

All replies

  • Probably you need something like this:

    Student result =

           students

                 .OrderByDescending( s => s.ClassCredit )

                 .ThenByDescending( s => s.Grade )

                 .FirstOrDefault( );

    (But what if there are several solutions?)


    • Edited by Viorel_MVP Thursday, August 13, 2020 7:28 PM
    • Proposed as answer by Naomi N Thursday, August 13, 2020 7:40 PM
    • Marked as answer by zydjohn Thursday, August 13, 2020 7:53 PM
    Thursday, August 13, 2020 7:25 PM
  • This should work

        var studentA1 = new Student
        {
            ID = 1,
            ClassCredit = 5,
            Grade = 90,
        };
    
        var studentA2 = new Student
        {
            ID = 2,
            ClassCredit = 5,
            Grade = 80,
        };
    
        var studentA3 = new Student
        {
            ID = 3,
            ClassCredit = 5,
            Grade = 70,
        };
    
        var studentB1 = new Student
        {
            ID = 4,
            ClassCredit = 10,
            Grade = 90,
        };
    
        var studentB2 = new Student
        {
            ID = 5,
            ClassCredit = 10,
            Grade = 80,
        };
    
        var studentB3 = new Student
        {
            ID = 6,
            ClassCredit = 10,
            Grade = 70,
        };
    
        var students = new List<Student>
        {
            studentA1,
            studentA2,
            studentA3,
            studentB1,
            studentB2,
            studentB3
        };
    
        var results = students
            .OrderByDescending(x => x.Grade)
            .ThenBy(x => x.ClassCredit).FirstOrDefault();
    
        Console.WriteLine($"{results.ID}, {results.ClassCredit}, {results.Grade}");

    Or

    var results = students
        .OrderByDescending(x => x.ClassCredit)
        .ThenBy(x => x.Grade).FirstOrDefault();


    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


    Thursday, August 13, 2020 7:28 PM