EF Core Projection Queries

This tutorial is about Projection queries in EF Core. We use them to create a query that selects specific columns from a set of entities. The projection queries create a query that selects from a set of entities in your model but returns results that are of a different type. This is also known as the query projection. The Projection Queries results can return an anonymous type or a Concrete type.

Database:
The Database for this tutorial is taken from the chinook database.
Source Code:
The source code of this project available in GitHub. It also contains the script of the database

Projection Queries in EF Core

What is Projection query

Consider the following query

SQL Query

The above query returns the list of customers from the customers table. The type (or entity Type) being queried here is Customer. This query returns the collection of Customers, which is the same as that of the type being queried.

Now, look at the query that EF Core sends to the database. It includes all the columns from the Customers table. While in our code we use only FirstName, LastName & Email. We do not need those extra columns. Depending on the size of the customer’s table, we may be moving a large amount of unnecessary data. It also comes with performance costs like CPU usage, I/O, network bandwidth, etc.

Hence it makes sense only to retrieve FirstName, LastName & Email. But we do not have a type which matches them. This is where EF Core Projection Query comes into play. The EF Core lets us create queries that retrieve only the selected fields and returns the Custom type.

There are two ways you can project the results of an EF Core Query. You can use projection to return a Concrete Type or an Anonymous Type.

Projecting into Concrete Type

To Project into a Concrete type, we first need to define a type with all the fields we want in the result as shown below.

Method Syntax

We use the Select method and a lambda expression to map the fields to a new customerModel. As you can see from the code below the final query contains only the required fields.

Best Entity Framework Core Books
The Best EF Core Books, which helps you to get started with EF Core  

SQL Query

Query Syntax

The following is the same query but in Query Syntax.

Projecting into Anonymous Types

Anonymous type is a C# feature that enables us to create variables without declaring their type. We use the var keyword to declare the Anonymous type. The compiler infers the properties of the type by its usage. They provide an easy way to create a new type without initializing them.

The Projection queries can load the data into the Anonymous Type as shown below.

Method Syntax

Query Syntax

References

Summary

The Projection queries improve the efficiency of your application by retrieving only the required data from the database. We can project to a concrete type or to an anonymous type

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top