Chapter 4. Querying, Inserting, Updating, and Deleting Data

In this chapter, we will learn how to query data in your database using Entity Framework and LINQ. We will understand the details of query life cycle. We will see how to use eager and lazy loading. We will also study how to sort and filter data. You will learn the use of relationships in our queries. We will add, update, and delete data in the database using multiple approaches for each operation.

In this chapter, we will cover the following topics:

  • How to use a method and the query syntax with LINQ
  • How to filter and sort data in your queries
  • Learn the pitfalls and advantages of lazy and eager loading
  • Cover multiple approaches for data manipulation

The basics of LINQ

Language INtegrated Query (LINQ) is the language that we use with Entity Framework to construct and execute queries against a database. A query is a statement that retrieves data from one or more tables. LINQ has many query implementations. At the most basic level, .NET includes LINQ in an object's functionality that allows you to query in-memory collections. LINQ to entities is typically the name that is used when talking about LINQ in relation to Entity Framework. This technology uses Entity Framework in conjunction with a provider for a specific RDBMS to convert LINQ statements to SQL queries. Entity Framework takes care of materialization; the process of converting the results of SQL queries into collections of .NET objects or individual objects.

When you use LINQ to entities queries, you will find out that the SQL is executed against the database when you enumerate the query results. Entity Framework converts LINQ queries to expression trees and then command trees and then they are passed to the provider, which finally executes a SQL query against the database engine it supports. For example, if you step through the following code while you have SQL Profiler running, you will see that the query will be run against the database when you step through the second line, but not the first line of code, as shown in the following code snippet:

var query = context.People;
var data = query.ToList();

The same code in VB.NET looks as follows:

Dim query = context.People
Dim data = query.ToList()

It may not be obvious from the first reading of this code, but the ToList function is the function that causes the enumeration of the query results to occur. The Entity Framework provider for SQL Server is used in the implementation of the IQueryable interface in our user case. Hence, when the GetEnumerator function is called on IQueryable, which is one of the interfaces that DbSet<T> implements, a SQL query is constructed and run by Entity Framework and the SQL Server provider for Entity Framework.

LINQ supports two types of query syntax:

  • The method syntax
  • The query syntax

It is entirely up to you which syntax you want to use, as there is parity between both syntaxes. As the name implies, the query syntax looks similar to SQL queries from the language perspective. The method syntax, on the other hand, looks like typical function calls in C# or VB.NET.

All of the following examples do not include the code that creates and disposes of DbContext for brevity. The context is stored in the context variable. We are going to use a database with a structure very similar to the one in Chapter 3, Defining the Database Structure, for all of the examples in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.36