MSDN .NET Documentation

Concepts: Data Access - ADO.NET - Entity Framework

Entity Framework Queries

This section delves into the powerful querying capabilities of Entity Framework (EF), a popular Object-Relational Mapper (ORM) for .NET. EF allows developers to interact with a relational database using .NET objects, abstracting away much of the complexity of SQL.

Introduction to LINQ to Entities

Entity Framework leverages Language Integrated Query (LINQ) to provide a type-safe and expressive way to query your data. LINQ to Entities translates your LINQ queries into SQL queries that are executed against your database.

Basic Query Syntax

Here's a simple example of how to retrieve all customers from a database:

using (var context = new YourDbContext()) { var allCustomers = from c in context.Customers select c; foreach (var customer in allCustomers) { Console.WriteLine($"Name: {customer.Name}, Email: {customer.Email}"); } }

Method Syntax

Alternatively, you can use the fluent method syntax:

using (var context = new YourDbContext()) { var allCustomers = context.Customers.ToList(); foreach (var customer in allCustomers) { Console.WriteLine($"Name: {customer.Name}, Email: {customer.Email}"); } }

Filtering Data

The Where clause or method is used to filter results based on specific criteria.

Example: Customers in a Specific City

using (var context = new YourDbContext()) { var customersInLondon = from c in context.Customers where c.City == "London" select c; // Or using method syntax: // var customersInLondon = context.Customers.Where(c => c.City == "London").ToList(); foreach (var customer in customersInLondon) { Console.WriteLine($"Name: {customer.Name}, City: {customer.City}"); } }

Sorting Data

Use the OrderBy and OrderByDescending clauses or methods to sort your results.

Example: Customers Sorted by Name

using (var context = new YourDbContext()) { var sortedCustomers = from c in context.Customers orderby c.Name ascending select c; // Or using method syntax: // var sortedCustomers = context.Customers.OrderBy(c => c.Name).ToList(); foreach (var customer in sortedCustomers) { Console.WriteLine($"Name: {customer.Name}"); } }

Projection

Projection allows you to select specific properties or create anonymous types from your query results.

Example: Retrieving Only Names and Emails

using (var context = new YourDbContext()) { var customerContactInfo = from c in context.Customers select new { c.Name, c.Email }; // Or using method syntax: // var customerContactInfo = context.Customers.Select(c => new { c.Name, c.Email }).ToList(); foreach (var info in customerContactInfo) { Console.WriteLine($"Name: {info.Name}, Email: {info.Email}"); } }

Joins

Entity Framework supports joins between related entities, often automatically inferred based on your model's relationships.

Example: Customers and Their Orders

using (var context = new YourDbContext()) { var customerOrders = from c in context.Customers join o in context.Orders on c.CustomerId equals o.CustomerId select new { CustomerName = c.Name, OrderId = o.OrderId, OrderDate = o.OrderDate }; // Or using method syntax: // var customerOrders = context.Customers // .Join(context.Orders, c => c.CustomerId, o => o.CustomerId, (c, o) => new { CustomerName = c.Name, OrderId = o.OrderId, OrderDate = o.OrderDate }) // .ToList(); foreach (var item in customerOrders) { Console.WriteLine($"Customer: {item.CustomerName}, Order ID: {item.OrderId}, Date: {item.OrderDate}"); } }

Database Functions and Stored Procedures

EF also provides mechanisms to call database functions and stored procedures directly.

Note: The specific implementation for calling stored procedures and functions can vary slightly depending on your EF version and database provider.

Lazy Loading, Eager Loading, and Explicit Loading

Understanding how related data is loaded is crucial for performance. Entity Framework offers:

Example: Eager Loading Orders with Customers

using (var context = new YourDbContext()) { var customersWithOrders = context.Customers .Include(c => c.Orders) // Eagerly load the Orders navigation property .ToList(); foreach (var customer in customersWithOrders) { Console.WriteLine($"Customer: {customer.Name}"); foreach (var order in customer.Orders) { Console.WriteLine($"- Order ID: {order.OrderId}, Date: {order.OrderDate}"); } } }
Tip: Use eager loading (Include()) when you know you will need the related data to avoid multiple round trips to the database.

Advanced Querying Techniques

Entity Framework supports many other advanced querying patterns, including:

Important: Always consider the performance implications of your queries. EF generates SQL, and inefficient LINQ can lead to inefficient SQL. Use tools like SQL Server Profiler or EF's logging to inspect generated SQL.

Exploring these query features will enable you to efficiently retrieve and manipulate data within your .NET applications using Entity Framework.