How to use Linq to find max value for multiple columns

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

zydjohn

Guest
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,

Continue reading...
 
Back
Top