Skip to content

Instantly share code, notes, and snippets.

@kw0006667
Created October 23, 2012 19:52
Show Gist options
  • Save kw0006667/3941138 to your computer and use it in GitHub Desktop.
Save kw0006667/3941138 to your computer and use it in GitHub Desktop.
從10個學生中取出成績和排序,並取出不及格的學生
namespace ListConsoleApp
{
public class Student
{
public string Name;
public int Score;
public Student(string name, int score)
{
this.Name = name;
this.Score = score;
}
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>(){ new Student("Student A", 90),
new Student("Student B", 75),
new Student("Student C", 83),
new Student("Student D", 94),
new Student("Student E", 60),
new Student("Student F", 56),
new Student("Student G", 30),
new Student("Student I", 73),
new Student("Student J", 68),
new Student("Student K", 46)};
foreach (var stu in students)
Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
students.Sort((x, y) => { return -x.Score.CompareTo(y.Score); });
// ---- 排序後(分數由高到低)
Console.WriteLine("\n----------排序後(分數由高到低)-------\n");
foreach (var stu in students)
Console.WriteLine("Name: {0}, Score: {1} ", stu.Name, stu.Score);
// ---- 不及格的學生
Console.WriteLine("\n------------不及格的學生------------\n");
var query = from stu in students
where stu.Score < 60
select stu;
foreach (var q in query)
Console.WriteLine("Name: {0}, Score: {1} ", q.Name, q.Score);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment