WISE OWL EXERCISES
VISUAL C# EXERCISES
- Creating forms (4)
- Coding form events (1)
- Laying out your code (2)
- C# variables (4)
- Enumerations and constants (2)
- Conditions (2)
- Modular code (3)
- Arrays (2)
- Looping (2)
- Files and folders (3)
- Properties in C# (3)
- Using lists (3)
- Validating forms (6)
- Toolbars, menus and status bars (1)
- FileDialogs and StreamReaders (1)
- Debugging and trapping errors (1)
- Introduction to DataGridViews (1)
- DataGridView events (3)
- Complex DataGridViews (2)
- Creating classes (4)
- The form as a class (1)
- Data structures (6)
- Inheritance (5)
- Interfaces (2)
- Delegates and events (2)
- Writing LINQ (2)
- Advanced LINQ (2)
- Entity Frameworks (1)
- LINQ with Entity Frameworks (4)
- Grouping using LINQ (2)
- LINQ to SQL (2)
Visual C# | Entity Frameworks exercise | Use LINQ to SQL to list authors and their count of books
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
The answer to the exercise will be included and explained if you attend the course listed below!
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
If you haven't already done so, go into SQL Server Management Studio and open the file in the above folder and run its script. This should generate a Books database.
Add a new LINQ to SQL classes item called Books to your project:
The item that you should add to your project.
In Server Explorer, create a connection to this database:
You should be able to expand the tables to show the tblAuthor and tblBook tables.
Drag these two tables onto your O/R designer window, and rename the tables to get this:
We've renamed the entities, but left all of the properties the same.
Create a new window called frmBooks, and within this create code to list out the authors. This should use the data context that you've created:
// use the classes in the LINQ to SQL data model
using (BooksDataContext books = new BooksDataContext())
Now use LINQ to SQL to list out all authors with the number of books they've written. The easiest way to do this is probably to create a new anonymous type with two properties:
- AuthorName - the first name and last name concatenated (with a space in between)
- BookCount - the number of books written by the author
This is what you should see accumulated when you run your code:
For each author you should see in parentheses the number of books they've written.
At this point you should be marvelling at how easy LINQ to SQL is to code!