Report top student grades - CSharp LINQ

CSharp examples for LINQ:Projection

Description

Report top student grades

Demo Code



using System;//w ww .  ja  v  a  2  s.  c o  m
using System.Collections.Generic;
using System.Linq;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
       List<Student> students = new List<Student>
      {
        // Use new object initialization syntax.
        new Student { Name = "XML", GPA = 2.8 }, new Student { Name = "J", GPA = 3.1 },
        new Student { Name = "MySQL", GPA = 3.2 }, new Student { Name = "G", GPA = 4.0 },
        new Student { Name = "MongoDB", GPA = 2.5 }, new Student { Name = "B", GPA = 1.4 },
        new Student { Name = "Arthur", GPA = 3.5 }, new Student { Name = "T", GPA = 1.0 }
      };
        var topStudents = from s in students
                          where s.GPA > 3.0
                          orderby s.GPA descending
                          select new { Name = s.Name, Grade = s.GPA };
        foreach (var student in topStudents)
        {
            Console.WriteLine("{0} : {1}", student.Name.PadRight(10), student.Grade);
        }

    }
}
class Student
{
    public string Name { get; set; }
    public double GPA { get; set; }
}

Result


Related Tutorials