Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

You're reading from  C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

Product type Book
Published in Nov 2023
Publisher Packt
ISBN-13 9781837635870
Pages 828 pages
Edition 8th Edition
Languages
Author (1):
Mark J. Price Mark J. Price
Profile icon Mark J. Price

Table of Contents (18) Chapters

Preface 1. Hello, C#! Welcome, .NET! 2. Speaking C# 3. Controlling Flow, Converting Types, and Handling Exceptions 4. Writing, Debugging, and Testing Functions 5. Building Your Own Types with Object-Oriented Programming 6. Implementing Interfaces and Inheriting Classes 7. Packaging and Distributing .NET Types 8. Working with Common .NET Types 9. Working with Files, Streams, and Serialization 10. Working with Data Using Entity Framework Core 11. Querying and Manipulating Data Using LINQ 12. Introducing Web Development Using ASP.NET Core 13. Building Websites Using ASP.NET Core Razor Pages 14. Building and Consuming Web Services 15. Building User Interfaces Using Blazor 16. Epilogue 17. Index

Introducing Web Development Using ASP.NET Core

The third and final part of this book is about web development using ASP.NET Core. You will learn how to build cross-platform projects such as websites, web services, and web browser apps.

Microsoft calls platforms for building applications app models or workloads.

I recommend that you work through this and subsequent chapters sequentially because later chapters will reference projects in earlier chapters, and you will build up sufficient knowledge and skills to tackle the trickier problems in later chapters.

In this chapter, we will cover the following topics:

  • Understanding ASP.NET Core
  • New features in ASP.NET Core
  • Structuring projects
  • Building an entity model for use in the rest of the book
  • Understanding web development

Understanding ASP.NET Core

Since this book is about C# and .NET, we will learn about app models that use them to build the practical applications that we will encounter in the remaining chapters of this book.

More Information: Microsoft has extensive guidance for implementing app models in its .NET Architecture Guides documentation, which you can read at the following link: https://dotnet.microsoft.com/en-us/learn/dotnet/architecture-guides.

Microsoft ASP.NET Core is part of a history of Microsoft technologies used to build websites and services that have evolved over the years:

  • Active Server Pages (ASP) was released in 1996 and was Microsoft’s first attempt at a platform for dynamic server-side execution of website code. ASP files contain a mix of HTML and code that execute on the server written in the VBScript language.
  • ASP.NET Web Forms was released in 2002 with the .NET Framework and was designed to enable non-web developers, such as...

Join our book community on Discord

https://packt.link/EarlyAccess

Qr code Description automatically generated

This chapter is about Language INtegrated Query (LINQ) expressions. LINQ is a set of language extensions that add the ability to work with sequences of data and then filter, sort, and project them into different outputs.This chapter will cover the following topics:

  • Writing LINQ expressions
  • LINQ in practice
  • Sorting and more
  • Using LINQ with EF Core
  • Joining, grouping, and lookups
  • Aggregating and paging sequences

Writing LINQ expressions

The first question we need to answer is a fundamental one: why does LINQ exist?

Comparing imperative and declarative language features

LINQ was introduced in 2008 with C# 3 and .NET Framework 3. Before that, if a C# and .NET programmer wanted to process a sequence of items, they had to use procedural, aka imperative, code statements. For example, a loop:

  1. Set the current position to the first item.
  2. Check if the item is one that should be processed by comparing one or more properties against specified values. For example, is the unit price greater than 50, or is the country equal to Belgium?
  3. If there’s a match, process that item. For example, output one or more of its properties to the user, update one or more properties to new values, delete the item, or perform an aggregate calculation like counting or summing values.
  4. Move on to the next item. Repeat until all items have been processed.

Procedural code tells the compiler how to achieve a goal. Do this...

LINQ in practice

Now we can build a console app to explore practical examples of using LINQ.

Understanding deferred execution

LINQ uses deferred execution. It is important to understand that calling most of the above extension methods does not execute the query and get the results. Most of these extension methods return a LINQ expression that represents a question, not an answer. Let's explore:

  1. Use your preferred code editor to create a new project, as defined in the following list:
    • Project template: Console App / console
    • Project file and folder: LinqWithObjects
    • Solution file and folder: Chapter11
  2. In the project file, globally and statically import the System.Console class.
  3. Add a new class file named Program.Helpers.cs.
  4. In Program.Helpers.cs, delete any existing statements and then define a partial Program class with a method to output a section title, as shown in the following code:
partial class Program
{
  private static void SectionTitle(string title)
  {
    ConsoleColor...

Sorting and more

Other commonly used extension methods are OrderBy and ThenBy, used for sorting a sequence.

Sorting by a single property using OrderBy

Extension methods can be chained if the previous method returns another sequence, that is, a type that implements the IEnumerable<T> interface.Let's continue working with the current project to explore sorting:

  1. In the FilteringUsingWhere method, append a call to OrderBy to the end of the existing query, as shown in the following code:
var query = names
  .Where(name => name.Length > 4)
  .OrderBy(name => name.Length);

Good Practice: Format the LINQ statement so that each extension method call happens on its own line, to make it easier to read.

  1. Run the code and note that the names are now sorted by shortest first, as shown in the following output:
Kevin 
Creed 
Dwight 
Angela 
Michael

To put the longest name first, you would use OrderByDescending.

Sorting by a subsequent property using ThenBy

We might want...

Using LINQ with EF Core

We have looked at LINQ queries that filter and sort, but none that change the shape of the items in the sequence. This is called projection because it's about projecting items of one shape into another shape. To learn about projection, it is best to have some more complex types to work with, so in the next project, instead of using string sequences, we will use sequences of entities from the Northwind sample database that you were introduced to in Chapter 10, Working with Data Using Entity Framework Core.I will give instructions to use SQLite because it is cross-platform, but if you prefer to use SQL Server then feel free to do so. I have included some commented code to enable SQL Server if you choose.

Creating a console app for exploring LINQ to Entities

First, we must create a console app and Northwind database to work with:

  1. Use your preferred code editor to add a new Console App / console project named LinqWithEFCore to the Chapter11 solution.
  2. In the...
lock icon The rest of the chapter is locked
You have been reading a chapter from
C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition
Published in: Nov 2023 Publisher: Packt ISBN-13: 9781837635870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}