diff --git a/Linq/Linq.cs b/Linq/Linq.cs index 6cabb44e..1097e839 100644 --- a/Linq/Linq.cs +++ b/Linq/Linq.cs @@ -1,12 +1,64 @@ using System; - +using System.Linq; +using System.Collections.Generic; namespace Linq { class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + List studentList = new List (); + + for (int i = 0; i < 10; i++) + { + studentList.Add(Student.generateRandom()); + } + Console.WriteLine("All the students:"); + foreach(var s in studentList) + { + Console.WriteLine(s); + } + + IEnumerable negativeBalance = from s in studentList + where s.tuition < 0 + select s; + Console.WriteLine("-----------------------------------"); + Console.WriteLine("Students with negative balance:"); + foreach(Student s in negativeBalance) + { + Console.WriteLine(s); + } + } + } + + public class Student + { + public string name; + public int tuition; + static Random randomGen = new Random(); + + override + public string ToString() + { + return name + ", tuition: " + tuition; + } + + public static Student generateRandom() + { + string randomName = ""; + int randomNameSize = randomGen.Next(10, 20); + for (int i = 0; i < randomNameSize; i++) + { + char randomLetter = (char)('A' + randomGen.Next(0, 26)); + randomName += randomLetter; + } + + int randomTuition = randomGen.Next(-5000, 5000); + Student randomStudent = new Student(); + randomStudent.name = randomName; + randomStudent.tuition = randomTuition; + return randomStudent; + } } }