# Find Highest in each group using LINQ

I’ll show you how to use LINQ to objects to find the maximum item in each group.

Consider this scenario: I have a list of students in this case, along with their grades and marks. Our aim is to determine the student, grade, and there max marks in each group.

• First Group by the Grade
• Then sort each group by Marks in descending order
• Then take first of each group
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Grade { get; set; }
public int Marks { get; set; }
}

void Main(string[] args)
{
var students = new List<Student>(){

};

var results = students.GroupBy(s => s.Grade)
.Select(d => d.OrderByDescending(d => d.Marks))
.Select(d => d.First());

foreach (var student in results)
{
Console.WriteLine(student);
}

}


Reactions