Exercise: LINQ to Objects
In this exercise you must use LINQ to Object.
- Query syntax: from teacher in Teachers where ... select ...
- Expression syntax: Teachers.Where(...).Select(...)
Very much like the example linqFirstTry.
Getting started
Create a new Solution in Visual Studio. Name the Solution LinqSchool (or something similar).
Model class Teacher
Create a simple class Teacher with properties Id, Name, and Salary.
Override the ToString(...) method in the Teacher class.
Main class
In the Main class (the class where you have your Main method) create a List of Teachers, with at least 4 Teacher objects.
All teachers
- Make a LINQ statements (query syntax) to return all teachers from the List of Teachers.
Print the result to the screen.
- Make a LINQ statement (expression syntax) to return all teacher ...
Print the result to the screen.
Teachers with a high salary
- Make a LINQ statements (query syntax) to return all teachers with a salary higher than some amount.
Print the result to the screen.
- Make a LINQ statement (expression syntax) to return all teacher ...
Print the result to the screen.
Teachers with a high salary, changing the enviromnent
- Make a LINQ statements (query syntax) to return all teachers with a salary higher than someAmount.
Change the value of someAmount.
Print the result to the screen.
- Make a LINQ statement (expression syntax) to return all teacher ...
Change the value of someAmount.
Print the result to the screen.
Ordering the result
- Make a LINQ statements (query syntax) to return all teachers ordered by name.
Print the result to the screen.
- Make a LINQ statement (expression syntax) to return all teacher ...
Print the result to the screen.
Joining Teacher and Course
In this part of the exercise you must join Teachers and Courses
Model class Course
Create a simple class Course with properties Name, Credits and TeacherId.
In the Main class create a List of Courses with at least 3 Courses.
Make sure the Courses TeacherId match an existing Teacher object.
Joining ...
- Make a LINQ statements (query syntax) to return {courseName, teacherName} for all courses.
Print the result to the screen.
- Extra: Make a LINQ statement (expression syntax) to return ...
Print the result to the screen.
Extra: Group by
Try LINQ group by.
Try to group Courses by TeacherId.