public static void StudentExtensionTest() { Student[] studentArray = { new Student() { StudentID = 1, StudentName = "John", Age = 18 }, new Student() { StudentID = 2, StudentName = "Steve", Age = 21 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Student() { StudentID = 4, StudentName = "Ram", Age = 20 }, new Student() { StudentID = 5, StudentName = "Ron", Age = 31 }, new Student() { StudentID = 6, StudentName = "Chris", Age = 17 }, new Student() { StudentID = 7, StudentName = "Rob", Age = 19 }, }; var students = StudentExtension.FindStudents(studentArray, delegate(Student std) { return(std.Age > 12 && std.Age < 20); }); foreach (var std in students) { if (std != null && !string.IsNullOrEmpty(std.StudentName)) { Console.WriteLine(std.StudentName); } } //Also, use another criteria using same delegate students = StudentExtension.FindStudents(studentArray, delegate(Student std) { return(std.StudentID == 5); }); }
static void Main(string[] args) { Console.WriteLine("LINQ Query to Array"); // Data source string[] names = { "Bill", "Steve", "James", "Mohan" }; // LINQ Query var myLinqQuery = from name in names where name.Contains('a') select name; // Query execution foreach (var name in myLinqQuery) { Console.WriteLine(name + " "); } Student[] studentArray = { new Student() { StudentID = 1, StudentName = "John", Age = 18 }, new Student() { StudentID = 2, StudentName = "Steve", Age = 21 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Student() { StudentID = 4, StudentName = "Ram", Age = 20 }, new Student() { StudentID = 5, StudentName = "Ron", Age = 31 }, new Student() { StudentID = 6, StudentName = "Chris", Age = 17 }, new Student() { StudentID = 7, StudentName = "Rob", Age = 19 }, }; Student[] students = StudentExtension.Where(studentArray, delegate(Student std) { return(std.Age > 12 && std.Age < 20); }); // Use LINQ to find teenager students Student[] teenAgerStudents = studentArray.Where(s => s.Age > 12 && s.Age < 20).ToArray(); // Use LINQ to find first student whose name is Bill Student bill = studentArray.Where(s => s.StudentName == "Bill").FirstOrDefault(); // Use LINQ to find student whose StudentID is 5 Student student5 = studentArray.Where(s => s.StudentID == 5).FirstOrDefault(); Console.WriteLine("LINQ Query Syntax, example 1:"); // string collection IList <string> stringList = new List <string>() { "C# Tutorials", "VB.NET Tutorials", "Learn C#", "MVC Tutorials", "Java" }; // LINQ Query Syntax var result = from s in stringList where s.Contains("Tutorials") select s; foreach (var str in result) { Console.WriteLine(str); } Console.WriteLine("LINQ Query Syntax, example 2:"); IList <Student> studentList = new List <Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 13 }, new Student() { StudentID = 2, StudentName = "Moin", Age = 21 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 18 }, new Student() { StudentID = 4, StudentName = "Ram", Age = 20 }, new Student() { StudentID = 5, StudentName = "Ron", Age = 15 } }; var teenAgerStudent = from s in studentList where s.Age > 12 && s.Age < 20 select s; Console.WriteLine("Teen age Students:"); foreach (Student std in teenAgerStudent) { Console.WriteLine(std.StudentName); } Console.ReadKey(); }
static void Main(string[] args) { List <Students> studentArray = new List <Students> { new Students() { StudentID = 1, StudentName = "John", Age = 18 }, new Students() { StudentID = 2, StudentName = "Steve", Age = 21 }, new Students() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Students() { StudentID = 4, StudentName = "Ram", Age = 20 }, new Students() { StudentID = 5, StudentName = "Ron", Age = 21 }, new Students() { StudentID = 6, StudentName = "Chris", Age = 17 }, new Students() { StudentID = 7, StudentName = "Rob", Age = 18 }, new Students() { StudentID = 8, StudentName = "John", Age = 22 }, new Students() { StudentID = 9, StudentName = "Ram", Age = 55 }, }; //using delegate as parameter List <Students> st = StudentExtension.where (studentArray, delegate(Students std) { return(std.Age > 12 && std.Age < 20); }); foreach (var item in st) { if (item != null) { Console.WriteLine(item.StudentName); } } //same result using lambda expression var result = studentArray.Select(a => a).Where(a => a.Age > 12 && a.Age < 20); PrintCollection(result); //operator ofType var stringResult = studentArray.OfType <string>(); Console.WriteLine("-----------OrderBy, ThenBy (ascending, descending)-----------"); //OrderBy, ThenBy (ascending, descending). Sort first Name i ascendig then sort by Age var orderThenBy = studentArray.OrderBy(x => x.StudentName).ThenByDescending(x => x.Age); Console.WriteLine("OrderBy, ThenBy (ascending, descending)"); PrintCollection(orderThenBy); Console.WriteLine("-----------Group by-----------"); //GroupBy na Lookup //only difference is GroupBy execution is deferred, whereas ToLookup execution is immediate GroupByLookUp grb = new GroupByLookUp(); grb.GroupByAge(studentArray); grb.LookUp(studentArray); Console.WriteLine("-----------Join-----------"); //Join outer and inner collection JoinStudents joins = new JoinStudents(); List <Courses> course = new List <Courses>() { new Courses() { CourseName = "C#", ID = 1 }, new Courses() { CourseName = "Java", ID = 2 }, new Courses() { CourseName = "Perl", ID = 3 }, new Courses() { CourseName = "Cplqs_plqs", ID = 4 }, }; joins.Joins(studentArray, course); Console.WriteLine("-----------GroupJoin-----------"); GroupJoin groupJoinObject = new GroupJoin(); groupJoinObject.GroupJopinInTwoCollection(studentArray); //more example groupJoinObject.SpeakLang(); Console.WriteLine("-----------Aggregate-----------"); List <string> ls = new List <string>() { "one", "two", "three", "four" }; string aggregate = ls.Aggregate((str1, str2) => str1 + "," + str2); Console.WriteLine(aggregate); Console.WriteLine("-----------Extension method: Except-----------"); OperatorExcept excObj = new OperatorExcept(); var excResult = excObj.groupOneOfStudents.Except(excObj.groupTwoOfStudents, new OperatorExcept.StudentComparer()); foreach (var item in excResult) { Console.WriteLine(item.StudentName); } Console.WriteLine("-----------Extension method: Skip/SkipWhile/Take/TakeWhile-----------"); int[] arrs = { 5, 1, 3, 22, 30, 60, 44, 55, 59, 38 }; var numbers = arrs.OrderBy(i => i). SkipWhile(i => i < 30). TakeWhile(i => i < 60); foreach (var number in numbers) { Console.Write(number + " "); } Console.WriteLine("-----------Extension method: ToDictionary-----------"); List <Students> studentsToDict = new List <Students>() { new Students() { StudentID = 1, StudentName = "Peshko", Age = 3 }, new Students() { StudentID = 2, StudentName = "Goshklo", Age = 4 }, new Students() { StudentID = 3, StudentName = "Penio", Age = 5 }, new Students() { StudentID = 4, StudentName = "Vladko", Age = 6 }, new Students() { StudentID = 5, StudentName = "Bai ivan", Age = 1 } }; var toDictionary = studentsToDict.ToDictionary <Students, int>(x => x.Age); List <string> list = new List <string>() { "cat", "dog", "animal" }; var animals = list.ToDictionary(x => x, x => true); if (animals.ContainsKey("dog")) { // This is in the Dictionary. Console.WriteLine("dog exists"); } }